你能解释一下 zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toInstant() 和 zonedDateTime.toInstant() 给出不同的输出吗?
Can you please explain when the zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toInstant() and zonedDateTime.toInstant() give different outputs?
有人可以说明这两种将 ZonedDateTime 调整为 UTC 的方法不同吗??如果可能,还提供一些测试事件日期时间。
String eventDate = "2016-11-28T10:56:28+11:00"; // my example date time
ZonedDateTime zonedDateTime = ZonedDateTime.parse(eventDate.trim(),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"));
// defaulting to UTC Zone
//1st way
System.out.println(zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toInstant());
//2nd way
System.out.println(zonedDateTime.toInstant());
my question was are these two ways any different for other date time inputs that might not necessarily have the same formats or timezone or offsets.
不可能有任何区别。你总是会从两种方式获得相同的瞬间。
原因是:一个ZonedDateTime
总是唯一定义一个时间点,一个瞬间。使用 withZoneSameInstant
转换到另一个时区后,新的 ZonedDateTime
将始终定义 相同的 时间点,相同的时刻。
BTW deHaar 在评论中是正确的:您的字符串包含与 UTC +11:00
的偏移量,并且没有时区,例如 Asia/Shanghai
,因此 OffsetDateTime
更合适class 比 ZonedDateTime
适合您的目的。
有人可以说明这两种将 ZonedDateTime 调整为 UTC 的方法不同吗??如果可能,还提供一些测试事件日期时间。
String eventDate = "2016-11-28T10:56:28+11:00"; // my example date time
ZonedDateTime zonedDateTime = ZonedDateTime.parse(eventDate.trim(),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"));
// defaulting to UTC Zone
//1st way
System.out.println(zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toInstant());
//2nd way
System.out.println(zonedDateTime.toInstant());
my question was are these two ways any different for other date time inputs that might not necessarily have the same formats or timezone or offsets.
不可能有任何区别。你总是会从两种方式获得相同的瞬间。
原因是:一个ZonedDateTime
总是唯一定义一个时间点,一个瞬间。使用 withZoneSameInstant
转换到另一个时区后,新的 ZonedDateTime
将始终定义 相同的 时间点,相同的时刻。
BTW deHaar 在评论中是正确的:您的字符串包含与 UTC +11:00
的偏移量,并且没有时区,例如 Asia/Shanghai
,因此 OffsetDateTime
更合适class 比 ZonedDateTime
适合您的目的。