为什么输出中出现减号?

Why is the minus sign present in the output?

我有以下几行代码:

LocalDateTime ldt = LocalDateTime.of(2017, 06, 02, 6, 0, 0);
ZoneOffset nyOffset = ZoneOffset.ofHoursMinutes(-5, 0);        
ZoneId nyZone = ZoneId.of("America/New_York");
OffsetDateTime nyOdt = ldt.atOffset(nyOffset);
ZonedDateTime nyZdt = ldt.atZone(nyZone);
Duration d = Duration.between(nyOdt, nyZdt);
System.out.println(d);

输出为PT-1H。为什么会这样? nyZdt 不在 nyOdt 之前。我错了吗?

如果您打印日期和相应的 UTC Instant,这会变得更清楚:

System.out.println(nyOdt);
System.out.println(nyZdt);
System.out.println(nyOdt.toInstant());
System.out.println(nyZdt.toInstant());

这将打印:

2017-06-02T06:00-05:00
2017-06-02T06:00-04:00[America/New_York]
2017-06-02T11:00:00Z
2017-06-02T10:00:00Z

请注意,nyOdt 使用偏移量 -05:00(比 UTC 晚 5 小时),但 nyZdt 使用 -04:00(比 UTC 晚 4 小时,由于 Daylight Saving Time in New York).

将它们转换为Instant的,可以看到nyOdt相当于UTC的上午11点,nyZdt相当于UTC的上午10点。这就是为什么两者之间的差异是 减去一小时Duration.between() returns 负持续时间 if the first parameter is after the second (和 nyOdt.toInstant().isAfter(nyZdt.toInstant()) returns true).