如何使用默认时区偏移量将Java ZonedDateTime转换为OffsetDateTime? [英] How can I convert Java ZonedDateTime to OffsetDateTime with default time zone offset?

查看:139
本文介绍了如何使用默认时区偏移量将Java ZonedDateTime转换为OffsetDateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我的ZonedDateTime为2018-10-30T18:04:58.874Z: 如何将其转换为OffsetDateTime 2018-10-30T13:04:58.874-05:00

Say I have a ZonedDateTime of 2018-10-30T18:04:58.874Z : How can I convert this to OffsetDateTime 2018-10-30T13:04:58.874-05:00

我希望将偏移量设置为默认/系统偏移量,例如从OffsetDateTime.now()中提取.

I'd prefer the offset to be the default/system offset, for example pulled from OffsetDateTime.now().

推荐答案

在您的ZonedDateTime中,您需要指定要在哪个其他区域中使用OffsetDateTime,精确定位该区域,然后使用.toOffsetDateTime():

From your ZonedDateTime you need to specify in which other zone you want your OffsetDateTime, precise the zone and them use .toOffsetDateTime() :

ZonedDateTime z = ZonedDateTime.parse("2018-10-30T18:04:58.874Z");
System.out.println(z); //2018-10-30T18:04:58.874Z

OffsetDateTime o = z.withZoneSameInstant(ZoneId.of("UTC-5")).toOffsetDateTime();
System.out.println(o); //2018-10-30T13:04:58.874-05:00

这篇关于如何使用默认时区偏移量将Java ZonedDateTime转换为OffsetDateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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