DateTimeParseException:无法解析文本:无法从TemporalAccessor获取LocalDateTime [英] DateTimeParseException: Text could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor

查看:1131
本文介绍了DateTimeParseException:无法解析文本:无法从TemporalAccessor获取LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 message looks like all values parsed correct, but anyway I see error.

如何使其正常工作?

How to make it working?

推荐答案

我只能重现尝试解析为 LocalDateTime ,所以我假设这就是您想要的。

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

您的错误是使用 hh (时钟小时-am-pm),而不是 HH (一天中的小时)。这有效:

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

打印:

2017-02-02T08:59:12

这篇关于DateTimeParseException:无法解析文本:无法从TemporalAccessor获取LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆