DateTimeParseException:无法解析文本:无法从 TemporalAccessor 获取 LocalDateTime
DateTimeParseException: Text could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor
LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
它打印错误:
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 消息看起来所有值都解析正确,但无论如何我都看到了错误。
如何让它发挥作用?
我只能重现当我尝试解析为 LocalDateTime
时出现的异常,所以我认为这就是您想要的。
您的错误是使用了 hh
(clock-hour-of-am-pm)而不是 HH
(一天中的小时)。这有效:
LocalDateTime ldt = LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt);
并打印:
2017-02-02T08:59:12
LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
它打印错误:
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 消息看起来所有值都解析正确,但无论如何我都看到了错误。
如何让它发挥作用?
我只能重现当我尝试解析为 LocalDateTime
时出现的异常,所以我认为这就是您想要的。
您的错误是使用了 hh
(clock-hour-of-am-pm)而不是 HH
(一天中的小时)。这有效:
LocalDateTime ldt = LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(ldt);
并打印:
2017-02-02T08:59:12