gpt4 book ai didi

java - 如何使用默认时区偏移量将 Java ZonedDateTime 转换为 OffsetDateTime?

转载 作者:行者123 更新时间:2023-12-04 14:51:59 25 4
gpt4 key购买 nike

假设我的 ZonedDateTime 为 2018-10-30T18:04:58.874Z :
如何将其转换为 OffsetDateTime 2018-10-30T13:04:58.874-05:00
我更喜欢偏移量是默认/系统偏移量,例如从 OffsetDateTime.now() 拉.

最佳答案

来自您的 ZonedDateTime您需要指定您希望在哪个其他区域使用 OffsetDateTime ,精确区域,他们使用 .toOffsetDateTime() :

ZonedDateTime z = ZonedDateTime.parse("2018-10-30T18:04:58.874Z");
System.out.println(z); //2018-10-30T18:04:58.874Z

OffsetDateTime o = z.withZoneSameInstant(ZoneId.of("UTC-5")).toOffsetDateTime();
System.out.println(o); //2018-10-30T13:04:58.874-05:00

关于java - 如何使用默认时区偏移量将 Java ZonedDateTime 转换为 OffsetDateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53071687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com