我只是试图将日期字符串转换为DateTime对象在Java中8.在运行以下行:无法解析LocalDateTime(Java 8)当从TemporalAccessor获得LocalDateTime
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
我得到以下错误:
Exception in thread "main" java.time.format.DateTimeParseException:
Text '20140218' could not be parsed:
Unable to obtain LocalDateTime from TemporalAccessor:
{},ISO resolved to 2014-02-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
的语法是相同的东西已建议here,但我有一个例外服务。我正在使用JDK-8u25
。
对于它的价值:即使使用'LocalDate'而不是'LocalDateTime',我也有同样的问题。问题是我使用'.withResolverStyle(ResolverStyle.STRICT);'创建了'DateTimeFormatter',所以我必须使用日期模式'uuuuMMdd'而不是'yyyyMMdd'(即“year”而不是“year-of-时代”)! – ZeroOne 2016-03-22 10:44:26
@ZeroOne谢谢你'uuuu'正是这个问题的答案,如果在严格模式下运行 – 2016-09-09 17:58:35
@ZeroOne这是一个严格的解析器的正确答案。非常感谢! – 2017-04-10 03:12:37