解析LocalDateTime时无法从TemporalAccessor获取LocalDateTime(Java 8)

ret*_*phy 122 java datetime datetime-format java-8

我只是想在Java 8中将日期字符串转换为DateTime对象.运行以下行:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime dt = LocalDateTime.parse("20140218", formatter);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

语法与此处的建议相同,但我遇到了一个例外.我在用JDK-8u25.

ret*_*phy 149

事实证明,Java不接受Date Date值作为DateTime.使用LocalDate而不是LocalDateTime解决了这个问题:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate dt = LocalDate.parse("20140218", formatter);
Run Code Online (Sandbox Code Playgroud)

  • 值得一提的是:即使使用`LocalDate`而不是`LocalDateTime`,我也遇到了同样的问题.问题是我使用`.ResolverStyle(ResolverStyle.STRICT);`创建了我的`DateTimeFormatter`,所以我不得不使用日期模式`uuuuMMdd`而不是`yyyyMMdd`(即"年"而不是"年份"时代")! (32认同)
  • @ZeroOne谢谢你'uuuu'正是这个问题的答案,如果在严格模式下运行 (5认同)

Øyv*_* Mo 61

如果您确实需要将日期转换为LocalDateTime对象,则可以使用LocalDate.atStartOfDay().这将在指定日期为您提供LocalDateTime对象,将小时,分钟和秒字段设置为0:

final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDateTime time = LocalDate.parse("20140218", formatter).atStartOfDay();
Run Code Online (Sandbox Code Playgroud)

  • `localDateTime.from`似乎没必要,因为`atStartOfDay()`已经返回`LocalDateTime`. (17认同)
  • 更正:设置为*当天开始时的任何时间*。 (2认同)
  • scala: val time = LocalDate.parse("20171220", DateTimeFormatter.ofPattern("yyyyMMdd")).atStartOfDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) (2认同)

小智 41

如果有人应该再次阅读这个主题(像我一样),那么正确的答案就是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(); 
Run Code Online (Sandbox Code Playgroud)

如果出现可选字段,则应设置它们.其余代码应完全相同.

  • 这是真正的通用解决方案,例如对于此类方法: Long getFileTimestamp(String file, Pattern pattern, DateTimeFormatter dtf, int group); 这里有不同的模式和 DateTimeFormetter,带和不指定时间。 (2认同)

Tom*_*m B 29

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

记录很少,相关的例外情况非常无益.


Zer*_*One 19

扩展了逆向学的答案 ..:即使使用LocalDate和不使用,我也有同样的问题LocalDateTime.问题是我创建了我的DateTimeFormatter使用.withResolverStyle(ResolverStyle.STRICT);,所以我不得不使用日期模式uuuuMMdd而不是yyyyMMdd(即"年"而不是"年代")!

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

(这个解决方案最初是对回顾的答案的评论,但我被鼓励将其作为一个独立的答案发布,因为它显然对许多人来说非常好.)


maw*_*urn 18

对于那些在这里遇到此错误的人,就像我做的那样:

Unable to obtain LocalDateTime from TemporalAccessor: {HourOfAmPm=0, MinuteOfHour=0}

它来自以下一行:

LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy h:mm"));
Run Code Online (Sandbox Code Playgroud)

事实证明,这是因为我在0小时使用12小时模式,而不是24小时模式.

使用大写H将小时更改为24小时模式修复它:

LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy H:mm"));
Run Code Online (Sandbox Code Playgroud)

  • 这个答案救了我的一天 (3认同)

pri*_*ime 10

如果日期字符串不包含小时,分钟等的任何值,则无法将其直接转换为a LocalDateTime.您只能将其转换为一个LocalDate,因为字符串 代表年份,月份和日期组件它是做正确的事.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate ld = LocalDate.parse("20180306", dtf); // 2018-03-06
Run Code Online (Sandbox Code Playgroud)

无论如何你可以将其转换为LocalDateTime.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate ld = LocalDate.parse("20180306", dtf);
LocalDateTime ldt = LocalDateTime.of(ld, LocalTime.of(0,0)); // 2018-03-06T00:00
Run Code Online (Sandbox Code Playgroud)


小智 5

 DateTimeFormatter format = new DateTimeFormatterBuilder()
                            .appendPattern("yyyy-MM-dd")
                            .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                            .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                            .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                            .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
                            .toFormatter();
Run Code Online (Sandbox Code Playgroud)

对我有用