D
D
Davidaa_WoW2021-12-06 22:45:11
Java
Davidaa_WoW, 2021-12-06 22:45:11

How to add time to a specific date as a string?

It means we have a string with a date inside, the date is not current, but arbitrary, in the format "yyyy.MM.dd hh:mm:ss", you need to add a certain amount of time to it, let's say half an hour. Since it is already in the form of a string, I think it would be possible to write logic for it, but for me it is too long and dreary process. On the Internet, I found a way to add time through a calendar, but it needs the Date type. The question is: either how to convert a String with the above format to a Date, or how to add the time in some other way.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2021-12-06
@Davidaa_WoW

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;


public class Main
{
  public static void main(String[] args) {
        String text = "2021.06.12 22:55:25";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
        LocalDateTime parsedDateTime = LocalDateTime.parse(text, formatter);
        LocalDateTime parsedDateTimePlus = parsedDateTime.plusMinutes(30);
        
        System.out.println(parsedDateTimePlus.format(formatter));
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question