无法将字符串解析为 Java 8 LocalDateTime

Abh*_*ngh 2 java datetime java-8

运行它会给我以下错误,我错过了什么?

public static void main(String[] args) {

    DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137",_timestampFomatGMT);
    System.out.println(localDateTime);     
}
Run Code Online (Sandbox Code Playgroud)

给了我以下例外。我错过了什么?

Exception in thread "main" java.time.format.DateTimeParseException: Text '20200331094118137' could not be parsed at index 0
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.LocalDateTime.parse(LocalDateTime.java:492)
        at cotown.lib.common.util.JavaTimeUtil.main(JavaTimeUtil.java:90)
Run Code Online (Sandbox Code Playgroud)

Shu*_*ham 6

Java 不接受简单的 Date 值作为 DateTime。

尝试使用 LocalDate,

public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDateTime = LocalDate.parse("20200331",_timestampFomatGMT);
System.out.println(localDateTime);
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您真的必须使用 LocalDateTime,请尝试

LocalDateTime time = LocalDate.parse("20200331", _timestampFomatGMT).atStartOfDay();
Run Code Online (Sandbox Code Playgroud)

编辑

已经提出了一个错误https://bugs.openjdk.java.net/browse/JDK-8031085。它在 JDK 9 中得到修复。