I get an unexpected exception when I try to parse a LocalDateTime (using JDK 8).
DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
String dateStr = LocalDateTime.now().format(DTF);
// => dateStr = 03/03/2023 01:56
LocalDateTime date = LocalDateTime.parse(dateStr, DTF);
// => throw DateTimeParseException
I don't undestand why the parse method throw a DateTimeParseException wheareas the format method work properly. I'm using the same DateTimeFormatter to format the date to string.
Exception in thread "main" java.time.format.DateTimeParseException: Text '03/03/2023 01:56' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=56, HourOfAmPm=1},ISO resolved to 2023-03-03 of type java.time.format.Parsed at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855) at java.time.LocalDateTime.parse(LocalDateTime.java:492)
Do you have an idea ?
I changed the pattern of DateTimeFormatter for hour-of-day (0-23).
DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
=> This work fine.
Why it doesn't work with the clock-hour-of-am-pm (1-12) ?