Have been beating my head against this and not sure what I'm doing wrong here.
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));
}
}
This code keeps printing "false" when it seems clear that the result should be "true".
What am I missing here? Would appreciate any guidance.
The answer by Jon Skeet is correct.
In Joda-Time
Just for fun, below is the solution in source-code using the third-party libary Joda-Time 2.3 in Java 7.
Details
The DateTimeZone class has a method, isStandardOffset. The only trick is that the method takes a long, the milliseconds backing the DateTime instance, accessed by calling DateTime class‘ superclass‘ (BaseDateTime) method getMillis.
Example Source Code
When run, note the difference in offset from UTC (-7 versus -8)…
About Joda-Time…
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.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.Additionally,
new Date(1380931200)
is actually in January 1970 - you meantnew Date(1380931200000L)
- don't forget that the number is milliseconds since the Unix epoch, not seconds.