ZonedDateTime.withZoneSameInstant 和 ZonedDateTime.withZoneSameLocal 有什么区别?

What is the difference between ZonedDateTime.withZoneSameInstant and ZonedDateTime.withZoneSameLocal?

假设我有一个 ZonedDateTime:

ZonedDateTime zonedDateTime = 
     ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("US/Pacific"));

我想知道 date/time 比方说在柏林。 我有两种方法:

zonedDateTime.withZoneSameInstant(ZoneId.of("Europe/Berlin")); // probably this is the right one to get the corresponding date/time in Berlin

zonedDateTime.withZoneSameLocal(ZoneId.of("Europe/Berlin"));

withZoneSameLocal 方法的文档说:"The local date-time is only changed if it is invalid for the new zone..." 并不清楚何时会真正发生(任何例子?=))。

date/time分别代表哪个,有什么区别?

如果要将时间戳从一个时区转换为另一个时区,请使用 withZoneSameInstant()withZoneSameLocal() 将更改区域但保持所有其他字段不变。例外情况是该时区的日期无效。

例如,

ZonedDateTime dtUTC = ZonedDateTime.parse("2019-03-10T02:30:00Z");
ZoneId pacific = ZoneId.of("US/Pacific");
System.out.println(dtUTC.withZoneSameInstant(pacific));
System.out.println(dtUTC.withZoneSameLocal(pacific));

prints

2019-03-09T18:30-08:00[US/Pacific]
2019-03-10T03:30-07:00[US/Pacific]

第一行是转换为另一个时区的原始时间戳。第二个 尝试 保留 date/time 字段,但 2:30 不是该日期的有效时间(因为夏令时跳跃),因此它改变了它一个小时。