Java 处理inDaylightTime()并确定日期是否为夏令时

Java 处理inDaylightTime()并确定日期是否为夏令时,java,timezone,dst,Java,Timezone,Dst,我一直在对这件事耿耿于怀,不知道我到底做错了什么 我正在测试某个时区的inDaylightTime()方法,但它返回“false”,而在本例中它应该返回“true” import java.util.TimeZone; import java.util.Date; public class TimeZoneDemo { public static void main( String args[] ){ Date date = new Date(1380931200);

我一直在对这件事耿耿于怀,不知道我到底做错了什么

我正在测试某个时区的inDaylightTime()方法,但它返回“false”,而在本例中它应该返回“true”

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));
    }    
}
当结果应该是“真”时,此代码会一直打印“假”


我错过了什么?非常感谢您的指导。

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

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


另外,
新日期(1380931200)
实际上是在1970年1月-你的意思是
新日期(138093120000L)
-不要忘记,这个数字是Unix纪元以来的毫秒,而不是秒。

你指定的时区是
GMT-8:00
-这是一个固定的时区,永远比UTC晚8小时。它不遵守夏时制

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


另外,
新日期(1380931200)
实际上是在1970年1月-你的意思是
新日期(138093120000L)
-不要忘了数字是Unix纪元以来的毫秒,而不是秒。

Jon Skeet的答案是正确的

在乔达时代 为了好玩,下面是使用Java7中的第三方libary2.3的源代码解决方案

细节 该类有一个方法。唯一的诀窍是,该方法需要很长的时间(毫秒)来支持DateTime实例,通过调用类'superclass'()方法来访问该实例

示例源代码 运行时,请注意与UTC的偏差(-7与-8)

约达时间

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

Jon Skeet的答案是正确的

在乔达时代 为了好玩,下面是使用Java7中的第三方libary2.3的源代码解决方案

细节 该类有一个方法。唯一的诀窍是,该方法需要很长的时间(毫秒)来支持DateTime实例,通过调用类'superclass'()方法来访问该实例

示例源代码 运行时,请注意与UTC的偏差(-7与-8)

约达时间

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

非常有趣,谢谢。不幸的是,我的“美国/洛杉矶”或“太平洋标准时间”仍然是“假”的。@kennym:看我的编辑-你的
日期也错了。请注意,真正的“PST”也不应该显示为夏令时,因为PST代表太平洋标准时间。不幸的是,这个缩写经常被误用为太平洋时间,它看起来就像内置的时区数据库一样,非常感谢。非常翔实的回答。我确实把日期弄错了。现在很好。非常有趣,谢谢。不幸的是,我的“美国/洛杉矶”或“太平洋标准时间”仍然是“假”的。@kennym:看我的编辑-你的
日期也错了。请注意,真正的“PST”也不应该显示为夏令时,因为PST代表太平洋标准时间。不幸的是,这个缩写经常被误用为太平洋时间,它看起来就像内置的时区数据库一样,非常感谢。非常翔实的回答。我确实把日期弄错了。现在一切正常。
// 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