在时区将ZonedDateTime转换为LocalDateTime

编程入门 行业动态 更新时间:2024-10-10 07:28:47
本文介绍了在时区将ZonedDateTime转换为LocalDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 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.

推荐答案

如何在瑞士的时区将其转换为LocalDateTime?

How can I convert it to LocalDateTime at time zone of Switzerland?

您可以将UTC转换为 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

本文发布于:2023-10-29 00:50:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   时区   ZonedDateTime   LocalDateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!