[英]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) ... 答案来自
ZoneId
的javadoc (强调我的)...
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#of
的javadoc :
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.