DST 与 Joda Time 一起过渡



我必须计算 Java 应用程序中的 DST 转换并查询堆栈溢出 我发现 joda time 有一个很好的 API 可以轻松做到这一点。但实际上它给了我错误的结果(或者至少看起来是这样)。

我编写了一个示例程序来打印过去 5 年和未来 5 年的过渡。

public static void main(final String[] args)
{
    TimeZone javaZone = TimeZone.getDefault();
    DateTimeZone jodaTimezone = DateTimeZone.forTimeZone(javaZone);
    Calendar[] dstTransitions;
    DateTime jodaNewYear;
    int dstSavings;
    final Calendar calendar = Calendar.getInstance(javaZone);
    final DateFormat df = DateFormat.getDateTimeInstance();
    // use the correct timezone for displaying the date and time on console
    df.setTimeZone(javaZone);
    calendar.setTimeInMillis(System.currentTimeMillis());

    for (int i = -5; i < 5; i++)
    {
        jodaNewYear = new DateTime(calendar.get(Calendar.YEAR) + i, 1, 1, 0, 0, 0, jodaTimezone);
        dstTransitions = new Calendar[2];
        dstTransitions[0] = Calendar.getInstance(javaZone);
        dstTransitions[0].setTimeInMillis(jodaTimezone.nextTransition(jodaNewYear.getMillis()));
        dstTransitions[1] = Calendar.getInstance(javaZone);
        dstTransitions[1].setTimeInMillis(jodaTimezone.nextTransition(dstTransitions[0].getTimeInMillis()));
        dstSavings = javaZone.getDSTSavings() / 1000;

        System.out.println("DST transitions for " + javaZone.getID() + " in " + jodaNewYear.getYear() + ":");
        System.out.println("To DST.....: " + df.format(dstTransitions[0].getTime()));
        System.out.println("From DST...: " + df.format(dstTransitions[1].getTime()));
        System.out.println("DST offset.: " + dstSavings + " sec");
        System.out.println("--------------------------------------------------------------------");
    }
}

这在我的机器上给了我以下输出(摘录):

DST transitions for Europe/Berlin in 2007:
To DST.....: 25.03.2007 03:00:00
From DST...: 28.10.2007 02:00:00
DST offset.: 3600 sec
--------------------------------------------------------------------
DST transitions for Europe/Berlin in 2008:
To DST.....: 30.03.2008 03:00:00
From DST...: 26.10.2008 02:00:00
DST offset.: 3600 sec
--------------------------------------------------------------------
DST transitions for Europe/Berlin in 2009:
To DST.....: 29.03.2009 03:00:00
From DST...: 25.10.2009 02:00:00
DST offset.: 3600 sec
--------------------------------------------------------------------
DST transitions for Europe/Berlin in 2010:
To DST.....: 28.03.2010 03:00:00
From DST...: 31.10.2010 02:00:00
DST offset.: 3600 sec
--------------------------------------------------------------------
DST transitions for Europe/Berlin in 2011:
To DST.....: 27.03.2011 03:00:00
From DST...: 30.10.2011 02:00:00
DST offset.: 3600 sec
--------------------------------------------------------------------
DST transitions for Europe/Berlin in 2012:
To DST.....: 25.03.2012 03:00:00
From DST...: 28.10.2012 02:00:00
DST offset.: 3600 sec

看起来还不错,但我对时代有问题。实际上,向 DST 的过渡发生在 3 月的最后一个星期日凌晨 2:00,而向后过渡发生在 10 月的最后一个星期日凌晨 3:00。所以乔达过渡的时代似乎是错误的。

我对 joda API 完全陌生,所以我可能忽略了一些东西。实际的问题是:如何确定正确的过渡时间?

问候

塞巴斯蒂安

过渡处存在不连续性。 25.03.12 02:0025.03.12 03:00相同,反之亦然,在10月,除了事情更复杂,因为02:00到03:00重复。

看起来乔达在过渡的"后期"一边给你时间——即过渡发生后的时间是什么。 要查找转换"之前"的时间,请按 DST 偏移量进行调整。

最新更新