How to convert string to LocalDateTime,how to solve string format "yyyymmddhhMMss" to LocalDateTime
String dateTime = "20221120000000";
LocalDateTime localDateTime = LocalDateTime.parse(dateTime);
Exception in thread "main" java.time.format.DateTimeParseException: Text '20221120000000' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at java.time.LocalDateTime.parse(LocalDateTime.java:477)
at com.company.Main.main(Main.java:21)
CodePudding user response:
This should do it:
String dateTimeString = "20221120000000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
If you dont want to use the DateTimeFormatter, your String needs to be in ISO format
CodePudding user response:
String dateTime = "20221120000000";
LocalDateTime localDateTime = LocalDateTime.parse(dateTime,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
I want it to convert to this format("yyyy-MM-dd HH:mm:ss") but I get the following error ;
Exception in thread "main" java.time.format.DateTimeParseException: Text '20221120000000' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at com.company.Main.main(Main.java:21)