获取 Java 中时区的夏令时转换日期

2022-09-02 12:06:51

我想知道最简单的方法,在Java中,获取未来夏令时将更改的日期列表。

一个相当不成熟的方法是简单地迭代一堆年的时间,根据TimeZone.inDaylightTime()测试它们。这将起作用,我不担心效率,因为它只需要每次我的应用程序启动时运行,但我想知道是否有更简单的方法。

如果你想知道我为什么要这样做,那是因为我有一个javascript应用程序,它需要处理包含UTC时间戳的第三方数据。我想要一种可靠的方式在客户端从GMT转换为EST。请参阅Javascript - Unix时间到特定时区我已经写了一些javascript可以做到这一点,但我想从服务器获得精确的过渡日期。


答案 1

Joda Time(一如既往)由于DateTimeZone.nextTransition方法,使这变得非常容易。例如:

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;
        }
    }
}

输出:

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
...

在 Java 8 中,您可以使用 ZoneRules 及其和方法获取相同的信息。nextTransitionpreviousTransition


答案 2

java.time

现代答案使用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());
    }

输出,缩写为:

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]

不过,我不会太信任数据。我不确定英国脱欧后(以及欧盟可能在2021年放弃夏令时(DST)之后)英国的时间会发生什么。

链接:Oracle教程:日期时间解释如何使用java.time。