2014-12-13 185 views
58

我只是试图将日期字符串转换为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

回答

80

事实证明,Java不接受一个纯粹的Date值作为DateTime。使用LocalDate而不是LocalDateTime解决了问题:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); 
LocalDate dt = LocalDate.parse("20140218", formatter); 
+17

对于它的价值:即使使用'LocalDate'而不是'LocalDateTime',我也有同样的问题。问题是我使用'.withResolverStyle(ResolverStyle.STRICT);'创建了'DateTimeFormatter',所以我必须使用日期模式'uuuuMMdd'而不是'yyyyMMdd'(即“year”而不是“year-of-时代”)! – ZeroOne 2016-03-22 10:44:26

+2

@ZeroOne谢谢你'uuuu'正是这个问题的答案,如果在严格模式下运行 – 2016-09-09 17:58:35

+0

@ZeroOne这是一个严格的解析器的正确答案。非常感谢! – 2017-04-10 03:12:37

21

这是一个非常不清楚且无益的错误消息。经过多次试验和错误后,我发现LocalDateTime会出现上述错误,如果您不尝试解析时间。通过使用LocalDate来代替,它可以正常工作。

这是记录不完善,相关的例外是非常无益的。

28

如果您确实需要将日期转换为LocalDateTime对象,则可以使用LocalDate.atStartOfDay()。这会给你一个LocalDateTime对象在指定的日期,具有时,分,第二场设置为0:

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd"); 
LocalDateTime time = LocalDateTime.from(LocalDate.parse("20140218", formatter).atStartOfDay()); 
+1

更正:设置为*任何时间将在当天开始*。 – Trejkaz 2016-01-04 23:27:26

+12

LocalDateTime.from似乎没有必要,因为atStartOfDay()已经返回LocalDateTime。 – Dariusz 2016-01-11 12:56:58

+0

scala: val time = LocalDate。解析(“20171220”,DateTimeFormatter.ofPattern(“yyyyMMdd”))。atStartOfDay.format(DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”)) – 2017-12-20 05:09:35

18

对于什么是值得如果有人再读这个话题(比如我),正确的答案应该在DateTimeFormatter的定义,例如:

private static DateTimeFormatter DATE_FORMAT = 
      new DateTimeFormatterBuilder().appendPattern("dd/MM/yyyy[ [HH][:mm][:ss][.SSS]]") 
      .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) 
      .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) 
      .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) 
      .toFormatter(); 

应该设置可选字段,如果他们将出现。其余的代码应该完全相同。

+1

这是真正的通用解决方案,例如: Long getFileTimestamp(String file,Pattern pattern,DateTimeFormatter dtf,int group); 在这里你有不同的模式和DateTimeFormetter,w /和w/o指定的时间。 – toootooo 2017-05-03 14:58:38

7

扩展在retrography's answer ..:我使用LocalDate而不是LocalDateTime时,甚至有同样的问题。问题是我用.withResolverStyle(ResolverStyle.STRICT);创建了我的DateTimeFormatter,所以我不得不使用日期模式uuuuMMdd而不是yyyyMMdd(即“year”而不是“era-year”)!

DateTimeFormatter formatter = new DateTimeFormatterBuilder() 
    .parseStrict() 
    .appendPattern("uuuuMMdd") 
    .toFormatter() 
    .withResolverStyle(ResolverStyle.STRICT); 
LocalDate dt = LocalDate.parse("20140218", formatter); 

(该解决方案最初将retrography的答案评论,但我鼓励后它作为一个单独的答案,因为它显然作品真的很好了很多人。)

相关问题