问题描述
为什么
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now.withZoneSameInstant(ZoneOffset.UTC)
.equals(now.withZoneSameInstant(ZoneId.of("UTC"))));
打印 false
?
我希望两个 ZonedDateTime
实例相等。
推荐答案
答案来自(强调我的)......
The answer comes from the javadoc of ZoneId
(emphasis mine) ...
- 固定偏移 - 与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.
...来自(强调我的):
... and from the javadoc of ZoneId#of
(emphasis mine):
参数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
.
当方法按照文档中的建议使用,使用等于
的比较将返回 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
:
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
检查是否为存储相同的日期时间,您可以使用方法改为:
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")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!