ZonedDateTime到UTC并应用了偏移量? [英] ZonedDateTime to UTC with offset applied?

查看:516
本文介绍了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并应用了偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆