Java 为什么 Calendar.get(Calendar.DST_OFFSET) 在夏令时给出 0?

问题描述

我有一个对应于 2021-07-05T18:00:00.000-04:00(东部夏令时)的 Calendar 对象。然而 Calendar.get(DST_OFFSET) 和 Calendar.getTimeZone().getDSTSavings() 都给出 0。它应该是 1 小时。我错过了什么或我出了什么问题?我使用的所有其他方法都返回预期值。

我正在使用 setTimeInMillis() 和 TimeZone 使用偏移量创建日历。这就是它不起作用的原因吗?显示的民用时代总是对的...

尽管我想使用新的 Java 时间,但我将 Java 用于 Android。最后我检查了只有最新版本的 Android 支持新的 Java 时间。他们最终可能会添加对旧版本的支持

解决方法

一个问题是输入定义了与 UTC 的偏移量,而不是具有特定规则的实时时区(例如如果完全应用 DST,如果应用了,DST 什么时候应用).

Calendar 显然无法处理这些规则,该类(可能还有整个 API)并非设计为如此。

这是在 Java 8 中引入 java.time 的原因之一。

以下是在像您这样的情况下使用 java.time 的一些示例:

public static void main(String[] args) {
    // example String in ISO format
    String dateString = "2021-07-05T18:00:00.000-04:00";
    // define your time zone
    ZoneId americaNewYork = ZoneId.of("America/New_York");
    // parse the (zone-less) String and add the time zone
    ZonedDateTime odt = OffsetDateTime.parse(dateString)
                                      .atZoneSameInstant(americaNewYork);
    // then get the rules of that zone
    long hours = americaNewYork.getRules()
                               // then get the daylight savings of the datetime
                               .getDaylightSavings(odt.toInstant())
                               // and get the full hours of the dst offset
                               .toHoursPart();
    
    // use a formatter to format the output (nearly) as desired
    System.out.println(odt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)
                        + " has a daylight saving offset of "
                        + hours);
}

这个打印

2021-07-05T18:00:00-04:00[America/New_York] has a daylight saving offset of 1

编辑:

您的评论让我提供了一个使用 long 作为输入的类似版本:

public static void main(String[] args) {
    // example String in ISO format
    long input = 1625522400000L;
    // create an Instant from the input
    Instant instant = Instant.ofEpochMilli(input);
    // define your time zone
    ZoneId americaNewYork = ZoneId.of("America/New_York");
    // then get the rules of that zone
    long hours = americaNewYork.getRules()
                               // then get the daylight savings of the Instant
                               .getDaylightSavings(instant)
                               // and get the full hours of the dst offset
                               .toHoursPart();
    
    // use a formatter to format the output (nearly) as desired
    System.out.println(ZonedDateTime.ofInstant(instant,americaNewYork)
                                    .format(DateTimeFormatter.ISO_ZONED_DATE_TIME)
                        + " has a daylight saving offset of "
                        + hours);
}

输出和上面的例子一样。

,

在 Java 错误 tracker 中,您会发现问题。

在“回退”期间,日历不支持消歧,并且给定的本地时间被解释为标准时间。 为避免 DST 意外更改为标准时间,请调用 add() 重置值。

您可以通过将 set() 替换为

来解决它
cal.add(Calendar.MINUTE,-cal.get(Calendar.MINUTE));