当我尝试转换时OffsetDateTime
to LocalDateTime
来自 java.time,我期望结果LocalDateTime
将更新为本地时区。所以,如果我有一个OffsetDateTime
of 2011-12-03T10:00:00Z
,我的本地时区是 UTC+2,我希望 LocalDateTime 是2011-12-03T12:00:00
,但我得到的是2011-12-03T10:00:00
。我用方法转换它toLocalDateTime()
that OffsetDateTime
有。看起来它只是截断日期,删除偏移部分,而不调整时间。
所以我想找到一种方法来获得LocalDateTime
它表示考虑到区域偏移的本地日期时间。按照这个例子,我想得到2011-12-03T12:00:00
LocalDateTime 将为您提供 OffsetDateTime 的挂钟时间。那是 10:00
您需要首先转换为您所在时区的 ZonedDateTime
像这样
OffsetDateTime off = OffsetDateTime.of(2011,12,3,10,00,0,0, ZoneOffset.UTC);
ZonedDateTime zoned = off.atZoneSameInstant(ZoneId.of("Europe/Athens"));
LocalDateTime athensWallTime = zoned.toLocalDateTime();
System.out.println(athensWallTime);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)