Java ZonedDateTime到即时转换 [英] Java ZonedDateTime to Instant conversion

查看:95
本文介绍了Java ZonedDateTime到即时转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算按照以下逻辑将ZonedDateTime转换为即时。

I am planning to convert ZonedDateTime to instant as per the below logic.

说,我在PST时区,当前时间是11A.M.如果我现在转换(截至2018年3月4日没有夏令时)并且toInstant将是7 P.M。

Say, I am in PST timeZone and the current time is 11A.M. If I convert now( no daylight saving as of today March 04 2018) and the toInstant will be 7P.M.

对于同一个上午11点,toInstant将返回6截至2018年4月4日的PM将被观察为夏令时。

For the same 11 A.M, the toInstant will return 6 P.M as of April 04 2018 as daylight saving will be observed.

因此,以下代码正确返回。

So, The below code returns correctly.

ZonedDateTime dateTime = ZonedDateTime.now();  --->>> March 04th 2018 at 11 A.M PST
dateTime.plusMonths(1).toInstant(); -->> returns April 04th 2018 at 6 PM PST as daylight saving will be observed

但是,

如果我转换为Instant然后再添加一个月,结果会有所不同。

The result will be different if i convert to Instant and then add a month.

Instant dateTime = ZonedDateTime.now().toInstant();  --->>> March 04th 2018 at 7 P.M UTC
dateTime.plus(1,ChronoUnit.MONTHS).toInstant(); -->> returns April 04th 2018 at 7 PM UTC ( but the actual time should be 6 PM UTC ). 

这没关系,因为我们已经转换为UTC,它只是从那里添加。

This is ok, as we already converted to UTC and it just adds from there.

因此,要包括夏令时,我需要将天数或月数或年数....添加到ZonedDateTime然后转换为Instant。

Hence, To include the daylight saving time, I need to add days or months or years .... to ZonedDateTime and then convert to Instant.

ZonedDateTime dateTime = ZonedDateTime.now();   ---> March 04th 2018 at 11A.M
dateTime.plusDays(10).toInstant();     ---> March 14th 2018 at 6P.M
dateTime.plusMonths(1).toInstant();     ---> April 04th 2018 at 6P.M

上述代码按预期工作。但是下面的一个没有返回6 P.M,但它返回7 P.M。

The above code works as expected. But the below one is not returning 6P.M, But it returns 7P.M.

dateTime.plusSeconds(org.joda.time.Period.days(1).multipliedBy(10).toStandardSeconds().getSeconds())
         .toInstant())  --> ---> March 14th 2018 at 7P.M

不确定,这有什么问题以及如何使其工作使用秒。

Not sure, what is wrong with this and how to make it work with seconds.

推荐答案

原因可在 ZonedDateTime 上课。对于方法 plusDays ,我们在方法文档中看到了这一点:

The cause is found in the documentation for the ZonedDateTime class. For the method plusDays we see this in the method documentation:


本地时间线,在当地日期时间添加天数。然后使用区域ID将其转换回ZonedDateTime以获取偏移量。

This operates on the local time-line, adding days to the local date-time. This is then converted back to a ZonedDateTime, using the zone ID to obtain the offset.

当转换回ZonedDateTime时,如果本地日期时间处于重叠状态,然后,如果可能,将保留偏移量,否则将使用较早的偏移量。如果在间隙中,本地日期时间将按间隙的长度向前调整。

When converting back to ZonedDateTime, if the local date-time is in an overlap, then the offset will be retained if possible, otherwise the earlier offset will be used. If in a gap, the local date-time will be adjusted forward by the length of the gap.

但是,在文档中我们看到 plusSeconds 方法:

However, in the documentation for the plusSeconds method we see this:


这在即时时间线上运行,这样增加一秒将始终是一秒后的持续时间。这可能导致本地日期时间改变一秒以外的数量。请注意,这与使用天数,月数和年数的方法不同。

This operates on the instant time-line, such that adding one second will always be a duration of one second later. This may cause the local date-time to change by an amount other than one second. Note that this is a different approach to that used by days, months and years.

因此,这两种方法的行为方式不同,在选择适合您目的的方法时,您需要考虑这一点。

So the two methods are designed to behave differently, and you need to consider this when choosing which method to use to suit your purpose.

这篇关于Java ZonedDateTime到即时转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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