解析LocalDateTime(Java 8)时无法从TemporalAccessor获取LocalDateTime [英] Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8)
本文介绍了解析LocalDateTime(Java 8)时无法从TemporalAccessor获取LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMdd);
LocalDateTime dt = LocalDateTime.parse(20140218,格式化程序);
我收到以下错误:
线程main中的异常java.time.format.DateTimeParseException:
无法解析文本'20140218':
无法从TemporalAccessor获取LocalDateTime:
{},ISO解析为2014-02-18类型为java.time.format.Parsed
在java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
在java.time .format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
在java.time.LocalDateTime.parse(LocalDateTime.java:492)
语法与建议相同这里,但我有一个例外。我正在使用 JDK-8u25
。
解决方案
不接受裸日期值作为DateTime。使用LocalDate而不是LocalDateTime解决问题:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(yyyyMMdd);
LocalDate dt = LocalDate.parse(20140218,格式化程序);
I am simply trying to convert a date string into a DateTime object in Java 8. Upon running the following lines:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
I get the following error:
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)
The syntax is identical to what has been suggested here, yet I am served with an exception. I am using JDK-8u25
.
解决方案
It turns out Java does not accept a bare Date value as DateTime. Using LocalDate instead of LocalDateTime solves the issue:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate dt = LocalDate.parse("20140218", formatter);
这篇关于解析LocalDateTime(Java 8)时无法从TemporalAccessor获取LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文