繁体   English   中英

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

[英]Java why does Calendar.get(Calendar.DST_OFFSET) give 0 during daylight savings time?

我有一个对应于 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 ,则何时应用 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 as 输入的类似版本:

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 错误跟踪器中,您会发现您的问题。

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

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

cal.add(Calendar.MINUTE, -cal.get(Calendar.MINUTE));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM