-1

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) ?

  • 4
    "What is the difference between hh and HH ?" <- Questions like this are usually best answered by reading the official documentation first: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns – OH GOD SPIDERS Mar 03 '23 at 14:16
  • 4
    "Why it does'nt work with the clock-hour-of-am-pm (1-12) ?" <- Because your String doesn't say whether the time is am or pm, so the time cannot be mapped to a single specific time. 1 in 12 hour format could either be 1 or 13 in 24 hour format. When parsing the time you get an exception because of this ambiguity. – OH GOD SPIDERS Mar 03 '23 at 14:18
  • Well for the difference between hh and HH you can take a look [here](https://stackoverflow.com/questions/17341214/difference-between-java-hhmm-and-hhmm-on-simpledateformat) – Tam Thai Mar 03 '23 at 14:19
  • The problem with clock-hour-of-am-pm (`hh`) is that with `01` Java cannot guess whether you intended 1 AM or 1 PM (AKA 13) and therefore refuses to construct a `LocalDateTime` for you. The other problem of course being that with hours 0 and 13 through 23 they will be formatted wrong, into the 1 - 12 range. – Ole V.V. Mar 04 '23 at 09:57

1 Answers1

2

As explained by @OH GOD SPIDERS, I needed to either add a to specify AM/PM or use HH to specify 24-hour format.

So to use the clock-hour-of-am-pm (1-12) format, add a:

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mma");

Or to use hour-of-day format (0-23), change hh to HH:

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
tdy
  • 36,675
  • 19
  • 86
  • 83
  • This is not an answer. It should be a comment, an edit to the original question, or a new question. – geocodezip Mar 03 '23 at 17:17
  • 1
    This is definitely an answer, but OP included some classic signs of a non-answer ("thanks" and "if I want to"). I just edited those out to avoid the confusion. – tdy Mar 04 '23 at 05:08