【问题标题】:Issue with inDaylightTime() and determing if a date is in daylight savings timeinDaylightTime() 出现问题并确定日期是否处于夏令时
【发布时间】:2013-11-20 18:13:26
【问题描述】:

一直在反对这一点,不知道我在这里做错了什么。

我正在测试某个时区的 inDaylightTime() 方法,但在这种情况下它应该返回“true”时却返回“false”。

import java.util.TimeZone;
import java.util.Date;

public class TimeZoneDemo {
    public static void main( String args[] ){

        Date date = new Date(1380931200); // Sat, 05 Oct 2013, within daylight savings time.

        System.out.println("In daylight saving time: " + TimeZone.getTimeZone("GMT-8:00").inDaylightTime(date));
    }    
}

当结果应该是“真”时,这段代码一直打印“假”。

我在这里缺少什么?非常感谢任何指导。

【问题讨论】:

    标签: java timezone dst


    【解决方案1】:

    您指定的时区为GMT-8:00 - 这是一个固定的时区,比 UTC永久晚 8 小时。它不遵守夏令时。

    如果您实际上是指太平洋时间,则应指定 America/Los_Angeles 作为时区 ID。请记住,不同时区会在一年中的不同时间在标准时间和夏令时之间切换。

    另外,new Date(1380931200) 实际上是 1970 年 1 月 - 你的意思是 new Date(1380931200000L) - 不要忘记这个数字是自 Unix 纪元以来的 毫秒,而不是 .

    【讨论】:

    • 非常有趣,谢谢。不幸的是,我仍然对“America/Los_Angeles”或“PST”感到“假”。
    • @kennym:查看我的编辑 - 你的 Date 也错了。请注意,真正的“PST”也不应该显示为夏令时,因为 PST 代表太平洋标准时间。不幸的是,这个缩写经常被误用来表示太平洋时间,而且看起来内置的时区数据库也是如此。
    • 非常感谢。非常翔实的回应。我确实把日期弄错了。现在工作正常。
    【解决方案2】:

    Jon Skeet 的回答是正确的。

    在乔达时代

    只是为了好玩,下面是在Java 7中使用第三方库Joda-Time 2.3的源代码解决方案。

    详情

    DateTimeZone 类有一个方法,isStandardOffset。唯一的技巧是该方法需要很长时间,支持 DateTime 实例的毫秒数,可通过调用 DateTime class‘ superclass‘ (BaseDateTime) 方法 getMillis 访问。

    示例源代码

    // © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
    
    org.joda.time.DateTimeZone losAngelesTimeZone = org.joda.time.DateTimeZone.forID("America/Los_Angeles");
    org.joda.time.DateTime theSecondAt6PM = new org.joda.time.DateTime( 2013, 11, 2, 18, 0, losAngelesTimeZone ) ;
    org.joda.time.DateTime theThirdAt6PM = new org.joda.time.DateTime( 2013, 11, 3, 18, 0, losAngelesTimeZone ) ; // Day when DST ends.
    
    System.out.println("This datetime 'theSecondAt6PM': " + theSecondAt6PM + " is in DST: " + losAngelesTimeZone.isStandardOffset(theSecondAt6PM.getMillis()));
    System.out.println("This datetime 'theThirdAt6PM': " + theThirdAt6PM + " is in DST: " + losAngelesTimeZone.isStandardOffset(theThirdAt6PM.getMillis()));
    

    运行时,注意与 UTC 的偏移量差异(-7 与 -8)……

    This datetime 'theSecondAt6PM': 2013-11-02T18:00:00.000-07:00 is in DST: false
    This datetime 'theThirdAt6PM': 2013-11-03T18:00:00.000-08:00 is in DST: true
    

    关于 Joda-Time…

    // Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
    // http://www.joda.org/joda-time/
    
    // Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
    // JSR 310 was inspired by Joda-Time but is not directly based on it.
    // http://jcp.org/en/jsr/detail?id=310
    
    // By default, Joda-Time produces strings in the standard ISO 8601 format.
    // https://en.wikipedia.org/wiki/ISO_8601
    
    // About Daylight Saving Time (DST): https://en.wikipedia.org/wiki/Daylight_saving_time
    
    // Time Zone list: http://joda-time.sourceforge.net/timezones.html
    

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 2013-05-25
      • 2011-04-30
      • 2010-11-06
      • 2023-04-05
      • 1970-01-01
      • 2010-09-27
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多