DateTimeParseException: Text could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor
Asked Answered
P

1

20
LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));

It prints error:

java.time.format.DateTimeParseException: Text '2017-02-02 08:59:12' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=59, NanoOfSecond=0, SecondOfMinute=12, MicroOfSecond=0, MilliOfSecond=0, HourOfAmPm=8},ISO resolved to 2017-02-02 of type java.time.format.Parsed

Accoeding message looks like all values parsed correct, but anyway I see error.

How to make it working?

Physician answered 2/5, 2017 at 7:48 Comment(5)
LocalDate or LocalDateTime? You indicate hours etc in your formatterFazio
working fine by me!Passable
The exception indicates that you try to parse to a LocalDateTime. The code you have in your question worksFazio
sorry, guys, correctedPhysician
The detail to note in the error message is HourOfAmPm=8. It does not say hour of day…Cladoceran
F
34

I can only reproduce the exception you get when I try to parse to a LocalDateTime, so I assume that's what you want.

Your mistake is using hh (clock-hour-of-am-pm) instead of HH (hour-of-day). This works:

LocalDateTime ldt = LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt);

And prints:

2017-02-02T08:59:12
Fazio answered 2/5, 2017 at 7:54 Comment(1)
Thanks, my mistake was in year pattern, I used week based year YYYY instead of yyyyIridosmine

© 2022 - 2024 — McMap. All rights reserved.