Question

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.

Was it helpful?

Solution

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 meant new Date(1380931200000L) - don't forget that the number is milliseconds since the Unix epoch, not seconds.

OTHER TIPS

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

// © 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()));

When run, note the difference in offset from UTC (-7 versus -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

About 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top