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

拥有回忆 提交于 2020-05-10 09:55:24

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/43732751/datetimeparseexception-text-could-not-be-parsed-unable-to-obtain-localdatetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!