【发布时间】:2021-09-10 05:58:36
【问题描述】:
我目前收到此错误,我真的不知道为什么
java.time.format.DateTimeParseException: Text '21:5:20' could not be parsed at index 3
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalTime.parse(LocalTime.java:441)
...
这是我用来解析的方法。
public static ZonedDateTime parse(String fecha, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
LocalTime date = LocalTime.parse(fecha, formatter);
return ZonedDateTime.of(LocalDateTime.of(LocalDate.now(), date), ZoneId.systemDefault());
}
我需要返回ZonedDateTime,因此我正在做我正在做的事情。
错误说它似乎从文件21:5:20 中读取了正确的时间,这看起来是有效的,但由于某种原因它无法解析它。
我真的不知道我做错了什么。与此类似的问题是指日期,而不是时间。
我知道这似乎是一个微不足道的问题,但我真诚地感谢 Java 专家的帮助。提前谢谢你。
【问题讨论】:
-
确保时间字符串中的所有时间元素都包含 2 位数字(如分钟中缺少的前导 0)。
-
@DevilsHnd 我无法更改输入,呵呵。数据是从某处读取的。
标签: java localtime zoneddatetime localdatetime