ZonedDateTime.parse 错误?
ZonedDateTime.parse bug?
给定一个 ZonedDateTime
,调用 toString
然后 ZonedDateTime.parse
returns 在某些情况下不同的时间。
这是一个具体的例子。代码在 Scala 中,但在 Java.
中没有什么不同
import java.time._
val t = 1193534827725L
val z = ZoneId.of("Europe/Paris")
val a = Instant.ofEpochMilli(t).atZone(z) // 2007-10-28T02:27:07.725+01:00[Europe/Paris]
val b = ZonedDateTime.parse(a.toString) // 2007-10-28T02:27:07.725+02:00[Europe/Paris]
a == b // returns false!
重新解析的值也有不同的epochMilli:
scala> List(a, b).map(_.toInstant.toEpochMilli)
res46: List[Long] = List(1193534827725, 1193531227725)
scala> List(a, b).map(_.toInstant.toEpochMilli == t)
res47: List[Boolean] = List(true, false)
使用 .format(DateTimeFormatter.ISO_ZONED_DATE_TIME)
而不是 .toString
也不起作用。
根据 Javadoc for Instant#atZone
遵从 https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#ofInstant-java.time.Instant-java.time.ZoneId-
Converting an instant to a zoned date-time is simple as there is only
one valid offset for each instant.
怎么回事?
编辑: 还值得一提的是,夏令时发生在采样时间 (ref) 左右,因此有两个 02:27:07.725
实例。正如您从 .toString
中看到的那样,偏移量是正确的,但解析似乎不符合它。
已在 Java 9.
中修复
参见JDK-8066982:ZonedDateTime.parse() returns DST 秋季过渡期间的错误 ZoneOffset
给定一个 ZonedDateTime
,调用 toString
然后 ZonedDateTime.parse
returns 在某些情况下不同的时间。
这是一个具体的例子。代码在 Scala 中,但在 Java.
中没有什么不同import java.time._
val t = 1193534827725L
val z = ZoneId.of("Europe/Paris")
val a = Instant.ofEpochMilli(t).atZone(z) // 2007-10-28T02:27:07.725+01:00[Europe/Paris]
val b = ZonedDateTime.parse(a.toString) // 2007-10-28T02:27:07.725+02:00[Europe/Paris]
a == b // returns false!
重新解析的值也有不同的epochMilli:
scala> List(a, b).map(_.toInstant.toEpochMilli)
res46: List[Long] = List(1193534827725, 1193531227725)
scala> List(a, b).map(_.toInstant.toEpochMilli == t)
res47: List[Boolean] = List(true, false)
使用 .format(DateTimeFormatter.ISO_ZONED_DATE_TIME)
而不是 .toString
也不起作用。
根据 Javadoc for Instant#atZone
遵从 https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#ofInstant-java.time.Instant-java.time.ZoneId-
Converting an instant to a zoned date-time is simple as there is only one valid offset for each instant.
怎么回事?
编辑: 还值得一提的是,夏令时发生在采样时间 (ref) 左右,因此有两个 02:27:07.725
实例。正如您从 .toString
中看到的那样,偏移量是正确的,但解析似乎不符合它。
已在 Java 9.
中修复参见JDK-8066982:ZonedDateTime.parse() returns DST 秋季过渡期间的错误 ZoneOffset