【发布时间】:2020-04-24 13:14:07
【问题描述】:
这是我用来使用 Instant.parse 解析字符串的代码,
String date = "2018-05-01T00:00:00";
Instant.parse(date)
并低于错误
java.time.format.DateTimeParseException: Text '2018-05-01T00:00:00' could not be parsed at index 19
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.Instant.parse(Instant.java:395)
我不能使用其他Instant,所以只能寻找解决方案!
【问题讨论】:
-
当地时间应该在哪个时区?没有时区,您无法解析到瞬间。如果它是 UTC,你可以在解析之前简单地在字符串的末尾添加一个“Z”。
-
我不确定,因为我是从第三方 api 响应中获得的!
-
你应该查看第三方api的文档。
-
2018-05-01T00:00:00没有定义一个瞬间,一个瞬间。如果您能找出隐含的时区或 UTC 偏移量,我们可以帮助您转换为Instant。不过,似乎一天中的时间并不重要?那么为什么不直接解析成LocalDate呢?
标签: java datetime java.time.instant