0

I have a string "2015-09-17T12:00". How can I convert this String to LocalDateTime in format "yyyy-MM-dd HH:mm" and then convert it back to String?

2 Answers2

1

you can Replace T with whitespace(s):

String str="2015-09-17T12:00";
str.replace("T"," ");

afterwards convert to Date using SimpleDateFormat;

AsSiDe
  • 1,826
  • 2
  • 15
  • 24
0

We can go with DateTimeFormatter, its thread safety. Refer to below url: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

sample program on converting String to Local Date Time

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd 'T' HH:mm");
LocalDateTime localDateTime = LocalDateTime.parse("2019-18-04 T 18:51", formatter);

Local Date time provides lot of method to get the hours or minutes or seconds https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html