为什么ZoneOffset.UTC!= ZoneId.of(" UTC")? [英] Why is ZoneOffset.UTC != ZoneId.of("UTC")?

查看:8622
本文介绍了为什么ZoneOffset.UTC!= ZoneId.of(" UTC")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

ZonedDateTime now = ZonedDateTime.now();
System.out.println(now.withZoneSameInstant(ZoneOffset.UTC)
        .equals(now.withZoneSameInstant(ZoneId.of("UTC"))));

打印 false

我希望两个 ZonedDateTime 实例相等。

推荐答案

答案来自 javadoc ZoneId (强调我的)......

The answer comes from the javadoc of ZoneId (emphasis mine) ...


ZoneId用于标识用于在
Instant和LocalDateTime之间进行转换的规则。有两种不同类型的ID:

A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime. There are two distinct types of ID:


  • 固定偏移 - 与UTC / Greenwich完全解析的偏移量,对所有本地使用相同的偏移量date-times

  • 地理区域 - 用于查找从UTC / Greenwich的偏移量的特定规则集的区域

大多数固定偏移量由ZoneOffset表示。 在任何ZoneId上调用normalized()
将确保固定的偏移ID将表示为
作为ZoneOffset。

Most fixed offsets are represented by ZoneOffset. Calling normalized() on any ZoneId will ensure that a fixed offset ID will be represented as a ZoneOffset.

...来自 javadoc ZoneId#of (强调我的):

... and from the javadoc of ZoneId#of (emphasis mine):


此方法解析生成ZoneId或ZoneOffset的ID。 如果ID为Z,则返回
ZoneOffset,或以+或 -
开头。

参数id指定为UTC,因此它将返回 ZoneId 偏移量,也以字符串形式显示:

The argument id is specified as "UTC", therefore it will return a ZoneId with an offset, which also presented in the string form:

System.out.println(now.withZoneSameInstant(ZoneOffset.UTC));
System.out.println(now.withZoneSameInstant(ZoneId.of("UTC")));

输出:

2017-03-10T08:06:28.045Z
2017-03-10T08:06:28.045Z[UTC]

当您使用等于方法进行比较时,检查对象是否等效。由于描述的差异,评估的结果是 false

As you use the equals method for comparison, you check for object equivalence. Because of the described difference, the result of the evaluation is false.

normalized() 方法按照文档中的建议使用,使用等于的比较将返回 true ,如 normalized()将返回相应的 ZoneOffset

When the normalized() method is used as proposed in the documentation, the comparison using equals will return true, as normalized() will return the corresponding ZoneOffset:


规范化时区ID,尽可能返回ZoneOffset。

Normalizes the time-zone ID, returning a ZoneOffset where possible.



now.withZoneSameInstant(ZoneOffset.UTC)
    .equals(now.withZoneSameInstant(ZoneId.of("UTC").normalized())); // true

如文档所述,如果使用Z+ 0作为输入ID,将返回 ZoneOffset 直接,无需调用 normalized()

As the documentation states, if you use "Z" or "+0" as input id, of will return the ZoneOffset directly and there is no need to call normalized():

now.withZoneSameInstant(ZoneOffset.UTC).equals(now.withZoneSameInstant(ZoneId.of("Z"))); //true
now.withZoneSameInstant(ZoneOffset.UTC).equals(now.withZoneSameInstant(ZoneId.of("+0"))); //true






检查是否为存储相同的日期时间,您可以使用 isEqual 方法改为:


To check if they store the same date time, you can use the isEqual method instead:

now.withZoneSameInstant(ZoneOffset.UTC)
    .isEqual(now.withZoneSameInstant(ZoneId.of("UTC"))); // true






样本

System.out.println("equals - ZoneId.of(\"UTC\"): " + nowZoneOffset
        .equals(now.withZoneSameInstant(ZoneId.of("UTC"))));
System.out.println("equals - ZoneId.of(\"UTC\").normalized(): " + nowZoneOffset
        .equals(now.withZoneSameInstant(ZoneId.of("UTC").normalized())));
System.out.println("equals - ZoneId.of(\"Z\"): " + nowZoneOffset
        .equals(now.withZoneSameInstant(ZoneId.of("Z"))));
System.out.println("equals - ZoneId.of(\"+0\"): " + nowZoneOffset
        .equals(now.withZoneSameInstant(ZoneId.of("+0"))));
System.out.println("isEqual - ZoneId.of(\"UTC\"): "+ nowZoneOffset
        .isEqual(now.withZoneSameInstant(ZoneId.of("UTC"))));

输出:

equals - ZoneId.of("UTC"): false
equals - ZoneId.of("UTC").normalized(): true
equals - ZoneId.of("Z"): true
equals - ZoneId.of("+0"): true
isEqual - ZoneId.of("UTC"): true

这篇关于为什么ZoneOffset.UTC!= ZoneId.of(" UTC")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆