【发布时间】:2020-07-11 22:35:24
【问题描述】:
运行它会给我以下错误,我错过了什么?
public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137",_timestampFomatGMT);
System.out.println(localDateTime);
}
给我以下异常。我错过了什么?
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)
【问题讨论】:
-
你没有给它
"20200331"这个字符串吗?你给它"20200331094118137",格式错误。 -
另外,你的格式
yyyyMMdd代表LocalDate,所以如果你想要一个LocalDateTime(在一天的开始?),你应该先把它解析成LocalDate,然后再转换到LocalDateTime。 -
您还想解析没有时间部分的 LocalDate。我建议使用
LocalDate.parse("....", formatter).atStartOfDay() -
道歉编辑了代码
-
编辑后,问题似乎与this 重复。你能确认一下吗?