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

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)

语法与这里建议的完全相同,但是我遇到了一个异常。我使用的是JDK-8u25。


当前回答

试试这个:

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy"); 
LocalDate fromLocalDate = LocalDate.parse(fromdstrong textate, dateTimeFormatter);

你可以添加任何你想要的格式。这对我很有用!

其他回答

对于那些像我一样犯了这个错误的人:

无法从TemporalAccessor获取LocalDateTime: {HourOfAmPm=0, MinuteOfHour=0}

它来自于下面这句话:

LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy h:mm"));

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

通过使用大写H将小时改为24小时模式可以修复它:

LocalDateTime.parse(date, DateTimeFormatter.ofPattern("M/d/yy H:mm"));

扩展retrography的答案..:我有同样的问题,即使使用LocalDate而不是LocalDateTime。问题是,我已经创建了我的DateTimeFormatter使用. withresolverstyle (ResolverStyle.STRICT);,所以我必须使用日期模式uuuuMMdd而不是yyyyMMdd(即。“year”而不是“year-of-era”)!

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

(这个解决方案最初是对retrography的答案的评论,但我被鼓励将其作为一个独立的答案发布,因为它显然对许多人都很有效。)

如果您只是想采用一种格式(无论它是否有时间),并希望解析为LocalDateTime,您可以执行以下操作。

LocalDateTime parseDateTime(String dateTime, DateTimeFormatter fmt) {
  return fmt.parse(dateTime, t -> {
    LocalDate date = t.query(TemporalQueries.localDate());
    LocalTime time = t.query(TemporalQueries.localTime());
    return LocalDateTime.of(date, time != null ? time : LocalTime.MIDNIGHT);
  });
}

我需要这个,因为我要将日期/时间模式作为自定义Spark UDF的参数。

对于值得的是,如果有人应该再次阅读这个主题(像我一样),正确的答案将在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(); 

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

编辑:有用的东西从wittyameta评论:

记住在调用appendPattern之后添加parsedefaults。否则它会给出DateTimeParseException

试试这个:

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd-yyyy"); 
LocalDate fromLocalDate = LocalDate.parse(fromdstrong textate, dateTimeFormatter);

你可以添加任何你想要的格式。这对我很有用!