java DateTimeParseException:无法解析文本:无法从 TemporalAccessor 获取 LocalDateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43732751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTimeParseException: Text could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor
提问by gstackoverflow
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.
Accoeding 消息看起来像所有解析的值都正确,但无论如何我看到错误。
How to make it working?
如何让它工作?
回答by Robin Topper
I can only reproduce the exception you get when I try to parse to a LocalDateTime
, so I assume that's what you want.
当我尝试解析为 a 时,我只能重现您得到的异常LocalDateTime
,所以我认为这就是您想要的。
Your mistake is using hh
(clock-hour-of-am-pm) instead of HH
(hour-of-day). This works:
您的错误是使用hh
(clock-hour-of-am-pm) 而不是HH
(hour-of-day)。这有效:
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