ZonedDateTime到UTC并应用了偏移量?

编程入门 行业动态 更新时间:2024-10-10 17:27:53
本文介绍了ZonedDateTime到UTC并应用了偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Java 8 这就是我的 ZonedDateTime 看起来像

2013-07-10T02:52:49+12:00

我得到这个值为

z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)

其中 z1 是 ZonedDateTime 。

我想将此值转换为 2013-07-10T14:52:49

我该怎么做?

推荐答案

<这是你想要的吗? 这会将 ZonedDateTime 转换为 LocalDateTime ,并且给定 ZoneId 之前将 ZonedDateTime 转换为 Instant 。

Is this what you want? This converts your ZonedDateTime to a LocalDateTime with a given ZoneId by converting your ZonedDateTime to an Instant before.

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneId.of("UTC"));

或许你想要用户系统时区而不是硬编码的UTC:

Or maybe you want the users system-timezone instead of hardcoded UTC:

LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneId.systemDefault());

更多推荐

ZonedDateTime到UTC并应用了偏移量?

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

发布评论

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

>www.elefans.com

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