我只是想在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来解析给定的日期字符串。您可以使用OOTB(开箱即用)、DateTimeFormatter。BASIC_ISO_DATE解析它。

演示:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.parse("20140218", DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(date);

        // In case you need an instance of LocalDateTime
        LocalDateTime ldt = date.atTime(LocalTime.MIN);
        System.out.println(ldt);
    }
}

输出:

2014-02-18
2014-02-18T00:00

在线演示

了解更多关于现代日期时间API*从Trail: Date Time。


*如果你正在为一个Android项目工作,而你的Android API级别仍然不兼容Java-8,检查Java 8+ API通过desugaring可用。注意,Android 8.0 Oreo已经提供了对java.time的支持。检查这个答案和这个答案,学习如何使用java。使用JDBC的时间API。

其他回答

试试这个:

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

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

如果您只是想采用一种格式(无论它是否有时间),并希望解析为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的参数。

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

无法从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"));

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

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

这很好

public class DateDemo {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm");
        String date = "16-08-2018 12:10";
        LocalDate localDate = LocalDate.parse(date, formatter);
        System.out.println("VALUE="+localDate);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
        LocalDateTime parse = LocalDateTime.parse(date, formatter1);
        System.out.println("VALUE1="+parse);
    }
}

输出:

VALUE=2018-08-16
VALUE1=2018-08-16T12:10