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?
Asked
Active
Viewed 263 times
0
-
5possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – AsSiDe Sep 26 '15 at 23:01
-
How do I get rid of that "T" in the String. – Sep 26 '15 at 23:06
-
1Doesn't the [DateTimeFormatter](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME) handle this? – MadProgrammer Sep 26 '15 at 23:12
-
Thanks, that works. :O – Sep 26 '15 at 23:32
2 Answers
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

Eswaran Venkatesan
- 163
- 2
- 7