简体   繁体   English

为什么ZoneOffset.UTC!= ZoneId.of(“UTC”)?

[英]Why is ZoneOffset.UTC != ZoneId.of(“UTC”)?

Why does 为什么

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

print out false ? 打印false

I would expect the both ZonedDateTime instances to be equal. 我希望两个ZonedDateTime实例相等。

The answer comes from the javadoc of ZoneId (emphasis mine) ... 答案来自ZoneIdjavadoc (强调我的)...

A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime. ZoneId用于标识用于在InstantDateTime和LocalDateTime之间进行转换的规则。 There are two distinct types of ID: 有两种不同类型的ID:

  • Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses the same offset for all local date-times 固定偏移 - 与UTC / Greenwich完全分辨的偏移量,对所有本地日期时间使用相同的偏移量
  • Geographical regions - an area where a specific set of rules for finding the offset from UTC/Greenwich apply 地理区域 - 用于查找从UTC /格林威治的偏移量的特定规则集适用的区域

Most fixed offsets are represented by ZoneOffset. 大多数固定偏移由ZoneOffset表示。 Calling normalized() on any ZoneId will ensure that a fixed offset ID will be represented as a ZoneOffset. 在任何ZoneId上调用normalized()将确保将固定的偏移ID表示为ZoneOffset。

... and from the javadoc of ZoneId#of (emphasis mine): ...来自(强调我的) ZoneId#ofjavadoc

This method parses the ID producing a ZoneId or ZoneOffset. 此方法解析生成ZoneId或ZoneOffset的ID。 A ZoneOffset is returned if the ID is 'Z', or starts with '+' or '-' . 如果ID为“Z”,则返回ZoneOffset,或者以“+”或“ - ”开头

The argument id is specified as "UTC" , therefore it will return a ZoneId with an offset, which also presented in the string form: 参数id指定为"UTC" ,因此它将返回带有偏移量的ZoneId ,该偏移量也以字符串形式显示:

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

Outputs: 输出:

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

As you use the equals method for comparison, you check for object equivalence . 当您使用equals方法进行比较时,您将检查对象的等效性 Because of the described difference, the result of the evaluation is false . 由于所描述的差异,评估结果是false

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 : 当在文档中提出使用normalized()方法时,使用equals的比较将返回true ,因为normalized()将返回相应的ZoneOffset

Normalizes the time-zone ID, returning a ZoneOffset where possible. 规范化时区ID,尽可能返回ZoneOffset。

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

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() : 作为文档状态,如果使用"Z""+0"作为输入ID, of将返回ZoneOffset直接,也没有必要调用normalized()

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

To check if they store the same date time , you can use the isEqual method instead: 要检查它们是否存储相同的日期时间 ,您可以使用isEqual方法:

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

Sample 样品

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"))));

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么理由使用 ZoneId.of("UTC") 而不是 ZoneOffset.UTC? - Is there any reason to use ZoneId.of("UTC") instead of ZoneOffset.UTC? 您能否解释一下 zonedDateTime.withZoneSameInstant(ZoneId.of(“UTC”)).toInstant() 和 zonedDateTime.toInstant() 何时给出不同的输出? - Can you please explain when the zonedDateTime.withZoneSameInstant(ZoneId.of(“UTC”)).toInstant() and zonedDateTime.toInstant() give different outputs? Java 8 ZoneOffset - 如何获取当前系统UTC偏移量 - Java 8 ZoneOffset - How to get current system UTC offset 如何使ZoneOffset UTC返回“+00:00”而不是“Z” - How to make ZoneOffset UTC return “+00:00” instead of “Z” 将具有zoneId的字符串本地日期转换为UTC Offsetdate JAVA - Convert string local date with zoneId to UTC Offsetdate JAVA Java 8:如何从 ZoneOffset 派生 ZoneId - Java 8: how to derive a ZoneId from ZoneOffset 如何在Java 8中正确使用ZoneOffset,ZoneId和LocalDateTime? - How to use ZoneOffset, ZoneId with LocalDateTime properly in Java 8? 有什么方法可以将 Java 8 中的 ZoneId 转换为 ZoneOffset? - Is there any way to convert ZoneId to ZoneOffset in Java 8? 从请求中获取 zoneID 以在 java 8 中将请求日期和时间转换为 UTC 格式 - Get zoneID from request to convert request date and time to UTC format in java 8 同时拥有 ZoneId 和 ZoneOffset 有意义吗? - Does having ZoneId and ZoneOffset at the same time make sense?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM