无法将字符串解析为Java 8 LocalDateTime [英] Unable to parse string into Java 8 LocalDateTime
本文介绍了无法将字符串解析为Java 8 LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
运行此命令会给我以下错误,我想念什么?
Running this gives me the following error, what am I missing ?
public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137",_timestampFomatGMT);
System.out.println(localDateTime);
}
给我以下异常.我想念什么?
Gives me the following exception. What am I missing ?
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)
推荐答案
Java不接受普通的Date值作为DateTime.
Java does not accept a plain Date value as DateTime.
尝试使用LocalDate,
Try using LocalDate,
public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDateTime = LocalDate.parse("20200331",_timestampFomatGMT);
System.out.println(localDateTime);
}
或者如果您确实需要使用LocalDateTime,请尝试
or if you really have to use LocalDateTime, then try
LocalDateTime time = LocalDate.parse("20200331", _timestampFomatGMT).atStartOfDay();
编辑
对此已经存在的错误 https://bugs.openjdk.java .net/browse/JDK-8031085 . 它在JDK 9中已修复.
there was a bug for this already raised https://bugs.openjdk.java.net/browse/JDK-8031085. It is fixed in JDK 9.
这篇关于无法将字符串解析为Java 8 LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文