JodaTime - 如何知道在特定时间段内是否发生夏令时? [英] JodaTime - how can I know whether a daylight saving occurs within a specified period of time?

查看:124
本文介绍了JodaTime - 如何知道在特定时间段内是否发生夏令时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道由

DateTime start;
DateTime end;

内有DST。

我正在迭代收集由{start,end}定义的时期,并在每次迭代中向前移动24小时的开始和结束。所产生的期间从午夜开始,在下一个午夜前1毫秒结束。我发现如果这段时间内有一个夏令时产生不正确的结果,例如:

I am iterating over collection of periods defined by {start,end} and shifting start and end 24 hours forward in every iteration. The resulting period starts at midnight and ends at 1 ms before next midnight. I found that if the period has a daylight saving point inside the shift produces incorrect result, e.g:

have:

Duration targetDuration = new Duration(24*60*60*1000L-1);
DateTime start = new DateTime("2012-03-10T00:00:00.000-08:00");
DateTime end = new DateTime("2012-03-10T23:59:59.999-08:00");   

然后轮班完成:

start = end.plusMillis(1);
end = start.plus(targetDuration);

产生:

start = "2012-03-11T00:00:00.000-08:00"
end = "2012-03-12T00:59:59.999-07:00"   

我想知道JodaTime中是否有任何标准API可以检查这段时间是否有DST?

I wonder is there any standard API in JodaTime that can check whether the period of time has a DST inside?

推荐答案

使用DateTimeZone.nextTransition方法。如果开始小于结束DateTime,则至少有一个时区过渡。这并不考虑规则更改与DST。也就是说,时区可能有一个新的规则,指出标准时间有一个新的偏移量,这将显示为时区转换。

Use the DateTimeZone.nextTransition method. If the start is less than the end DateTime, then at least one time zone transition has occurred in between. This does not account for rule changes vs. DST. That is, a time zone might have a new rule indicating that standard time has a new offset, and this would appear as a time zone transition.

if (start.getZone().nextTransition(start.getMillis()) < end.getMillis()) {
    // Time zone transition occurred, possibly due to DST
    ...
}

这篇关于JodaTime - 如何知道在特定时间段内是否发生夏令时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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