Java Instant.parse on Date java 8

标签 java time kml datetime-parsing java-time

我有一些包含时间戳条目的遗留 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/

相关文章:

java - int 到 Java 中的 unsigned char 数组

java - AlertDialog 内的 LinearLayout 不滚动

xml - kml 默认命名空间与数据集默认命名空间冲突

php - 将时间:[Distance,速度]数据转换为距离:[Time,速度]数据

无需专用函数即可将纪元时间转换为人类可读

kml - 对 kml 文件中的单轨进行着色

iphone - CLLocationManager Simulator 来模拟汽车移动?

java 日历格式

java - Hadoop 分布差异

php - 在mysql中为日期和时间选择数据类型