获取 Java 中时区的夏令时转换日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1449500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Daylight Saving Transition Dates For Time Zones in Java
提问by morgancodes
I'd like to know the simplest way, in Java, to get a list of dates in the future where daylight savings time will change.
我想知道在 Java 中最简单的方法来获取未来夏令时将发生变化的日期列表。
One rather inellegant way to do this would be to simply iterate over a bunch of years' worth of days, testing them against TimeZone.inDaylightTime(). This will work, and I'm not worried about efficiency since this will only need to run every time my app starts, but I wonder if there's a simpler way.
一种相当不优雅的方法是简单地迭代几年的天数,根据 TimeZone.inDaylightTime() 测试它们。这会奏效,而且我不担心效率,因为它只需要在我的应用程序每次启动时运行,但我想知道是否有更简单的方法。
If you're wondering why I'm doing this, it's because I have a javascript app which needs to handle third-party data containing UTC timestamps. I want a reliable way to translate from GMT to EST on the client side. See Javascript -- Unix Time to Specific Time ZoneI've written some javascript which will do it, but I want to get precise transition dates from the server.
如果您想知道我为什么要这样做,那是因为我有一个 javascript 应用程序需要处理包含 UTC 时间戳的第三方数据。我想要一种在客户端从 GMT 转换为 EST 的可靠方法。请参阅Javascript -- Unix 时间到特定时区我已经编写了一些 javascript 可以做到这一点,但我想从服务器获取精确的转换日期。
回答by Jon Skeet
Joda Time(as ever) makes this really easy due to the DateTimeZone.nextTransitionmethod. For example:
由于该DateTimeZone.nextTransition方法,Joda Time(一如既往)使这变得非常容易。例如:
import org.joda.time.*;
import org.joda.time.format.*;
public class Test
{
public static void main(String[] args)
{
DateTimeZone zone = DateTimeZone.forID("Europe/London");
DateTimeFormatter format = DateTimeFormat.mediumDateTime();
long current = System.currentTimeMillis();
for (int i=0; i < 100; i++)
{
long next = zone.nextTransition(current);
if (current == next)
{
break;
}
System.out.println (format.print(next) + " Into DST? "
+ !zone.isStandardOffset(next));
current = next;
}
}
}
Output:
输出:
25-Oct-2009 01:00:00 Into DST? false 28-Mar-2010 02:00:00 Into DST? true 31-Oct-2010 01:00:00 Into DST? false 27-Mar-2011 02:00:00 Into DST? true 30-Oct-2011 01:00:00 Into DST? false 25-Mar-2012 02:00:00 Into DST? true 28-Oct-2012 01:00:00 Into DST? false 31-Mar-2013 02:00:00 Into DST? true 27-Oct-2013 01:00:00 Into DST? false 30-Mar-2014 02:00:00 Into DST? true 26-Oct-2014 01:00:00 Into DST? false 29-Mar-2015 02:00:00 Into DST? true 25-Oct-2015 01:00:00 Into DST? false ...
With Java 8, you can get the same information using ZoneRuleswith its nextTransitionand previousTransitionmethods.
使用 Java 8,您可以使用ZoneRules它的nextTransition和previousTransition方法获得相同的信息。
回答by Ole V.V.
java.time
时间
The modern answer uses java.time, the modern Java date and time API.
现代答案使用 java.time,现代 Java 日期和时间 API。
ZoneId zone = ZoneId.of("Europe/London");
ZoneRules rules = zone.getRules();
ZonedDateTime now = ZonedDateTime.now(zone);
ZoneOffsetTransition transition = rules.nextTransition(now.toInstant());
Instant max = now.plusYears(15).toInstant();
while (transition != null && transition.getInstant().isBefore(max)) {
System.out.println(transition);
transition = rules.nextTransition(transition.getInstant());
}
Output, abbreviated:
输出,缩写:
Transition[Overlap at 2019-10-27T02:00+01:00 to Z] Transition[Gap at 2020-03-29T01:00Z to +01:00] Transition[Overlap at 2020-10-25T02:00+01:00 to Z] Transition[Gap at 2021-03-28T01:00Z to +01:00] Transition[Overlap at 2021-10-31T02:00+01:00 to Z] Transition[Gap at 2022-03-27T01:00Z to +01:00] Transition[Overlap at 2022-10-30T02:00+01:00 to Z] (cut) Transition[Overlap at 2033-10-30T02:00+01:00 to Z] Transition[Gap at 2034-03-26T01:00Z to +01:00]
Transition[Overlap at 2019-10-27T02:00+01:00 to Z] Transition[Gap at 2020-03-29T01:00Z to +01:00] Transition[Overlap at 2020-10-25T02:00+01:00 to Z] Transition[Gap at 2021-03-28T01:00Z to +01:00] Transition[Overlap at 2021-10-31T02:00+01:00 to Z] Transition[Gap at 2022-03-27T01:00Z to +01:00] Transition[Overlap at 2022-10-30T02:00+01:00 to Z] (cut) Transition[Overlap at 2033-10-30T02:00+01:00 to Z] Transition[Gap at 2034-03-26T01:00Z to +01:00]
I would not put too much trust in the data, though. I am not sure what happens with time in the UK after Brexit (and after EU may abandon summer time (DST) in 2021).
不过,我不会太相信数据。我不确定英国退欧后(以及欧盟可能在 2021 年放弃夏令时 (DST) 后)随着时间的推移会发生什么。
Link:Oracle tutorial: Date Timeexplaining how to use java.time.
链接:Oracle 教程:解释如何使用 java.time 的日期时间。

