gpt4 book ai didi

Java Instant.parse on Date java 8

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:05:16 26 4
gpt4 key购买 nike

我有一些包含时间戳条目的遗留 KML 文档。为什么使用 Instant 解析时以下日期无效?这两种方法都假定解析 ISO 8601 格式的日期。

字符串日期字符串 = "2017-12-04T08:06:60Z"

使用

java.time.Instant.parse(dateString)

抛出一个错误

"DateTimeParseException Text 2017-12-04T08:06:60Z could not be parsed at index 0."

但是,当使用

Date myDate =   javax.xml.bind.DatatypeConverter.parseDateTime( dateString )

myDate 被正确解析....

最佳答案

  1. 60 秒不是有效时间。这意味着这是无效的 2017-12-04T08:06:60Z,如果它是 60 秒,那么分钟应该增加并且你的时间将是 2017- 12-04T08:07:00Z
  2. 使用有效日期然后解析 String 就可以了:

    String date = "2017-12-04T08:07:00Z";
    System.out.println(Instant.parse(date));

java.time 也忽略 leap 秒。来自docs :

Implementations of the Java time-scale using the JSR-310 API are not required to provide any clock that is sub-second accurate, or that progresses monotonically or smoothly. Implementations are therefore not required to actually perform the UTC-SLS slew or to otherwise be aware of leap seconds. JSR-310 does, however, require that implementations must document the approach they use when defining a clock representing the current instant. See Clock for details on the available clocks.

关于Java Instant.parse on Date java 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55304194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com