假设我有一个ZonedDateTime:
ZonedDateTime zonedDateTime =
ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("US/Pacific"));我想知道几号/几点,比如说在柏林。我有两种方法:
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方法的文档说:“只有当新区域无效时,本地日期时间才会更改.”现在还不清楚这种情况何时会发生(任何例子吗?=)。
它们各自代表的日期/时间,有什么不同?
发布于 2019-03-06 22:05:33
如果要将时间戳从一个时区转换为另一个时区,请使用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));2019-03-09T18:30-08:00[US/Pacific]
2019-03-10T03:30-07:00[US/Pacific]第一行是转换到另一个时区的原始时间戳。第二个尝试保留日期/时间字段,但2:30不是该日期的有效时间(因为夏令节约),因此它将其移动了一个小时。
https://stackoverflow.com/questions/55032725
复制相似问题