inDaylightTime()问题,并确定日期是否在夏令时 [英] Issue with inDaylightTime() and determing if a date is in daylight savings time
问题描述
对此一直心存怀疑,不确定在这里做错了什么。
Have been beating my head against this and not sure what I'm doing wrong here.
我正在一定时间范围内测试inDaylightTime()方法但在这种情况下应返回 true时返回 false。
I am testing out the inDaylightTime() method for a certain time zone but it is returning "false" when it should be returning "true" in this case.
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));
}
}
当看起来很清楚时,此代码将继续打印 false
This code keeps printing "false" when it seems clear that the result should be "true".
我在这里错过了什么?希望获得任何指导。
What am I missing here? Would appreciate any guidance.
推荐答案
您指定的时区为 GMT-8:00
-这是一个固定时区,比UTC 永久晚8个小时。
You're specifying a time zone of GMT-8:00
- that's a fixed time zone, which is 8 hours behind of UTC permanently. It doesn't observe daylight saving time.
如果您实际上是指太平洋时间,则应指定 America / Los_Angeles
作为时区ID。请记住,一年中的不同时间会在标准时间和夏时制之间切换不同的时间。
If you actually meant Pacific Time, you should specify America/Los_Angeles
as the time zone ID. Bear in mind that different time zones switch between standard and daylight saving time at different times of the year.
此外,新日期(1380931200)
实际上是1970年1月-您的意思是新日期(1380931200000L)
-不要忘记该数字为毫秒 Unix时代,而不是 seconds 。
Additionally, new Date(1380931200)
is actually in January 1970 - you meant new Date(1380931200000L)
- don't forget that the number is milliseconds since the Unix epoch, not seconds.
这篇关于inDaylightTime()问题,并确定日期是否在夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!