问题描述
我有一个 ZonedDateTime
对象,它的构造如下
I have an object of ZonedDateTime
that is constructed like this
ZonedDateTime z = ZonedDateTime.of(LocalDate.now().atTime(11, 30), ZoneOffset.UTC);
如何将其转换为 LocalDateTime
瑞士的时区?预期结果应为 2018年4月16日13:30
。
How can I convert it to LocalDateTime
at time zone of Switzerland? Expected result should be 16 april 2018 13:30
.
推荐答案
您可以将UTC转换为<$ c将$ c> ZonedDateTime 转换为具有瑞士时区的 ZonedDateTime
,但保持相同的时间,然后获取 LocalDateTime
,如果需要的话。我很想将其保留为 ZonedDateTime
,除非出于某些其他原因而需要将其保留为 LocalDateTime
。
You can convert the UTC ZonedDateTime
into a ZonedDateTime
with the time zone of Switzerland, but maintaining the same instant in time, and then get the LocalDateTime
out of that, if you need to. I'd be tempted to keep it as a ZonedDateTime
unless you need it as a LocalDateTime
for some other reason though.
ZonedDateTime utcZoned = ZonedDateTime.of(LocalDate.now().atTime(11, 30), ZoneOffset.UTC);
ZoneId swissZone = ZoneId.of("Europe/Zurich");
ZonedDateTime swissZoned = utcZoned.withZoneSameInstant(swissZone);
LocalDateTime swissLocal = swissZoned.toLocalDateTime();
这篇关于在时区将ZonedDateTime转换为LocalDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!