views:

465

answers:

3

I have a Java Date that is from this summer during daylight savings time. For example:

Jun 01, 2009 06:00 AM PDT

My question is, how do I display this date as my local time zone and current daylight savings mode (in my case Pacific Standard Time)?

Jun 01, 2009 05:00 AM PST

Java's Date.toString() and SimpleDateFormat displays the date in the original daylight savings mode. Example:

System.out.println(new Date(1243861200000L));

outputs:

Mon Jun 01 06:00:00 PDT 2009

DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm aa zzz");
dateFormat.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(dateFormat.format(new Date(1243861200000L)));

outputs:

Jun 01, 2009 06:00 AM PDT

What I really want to see is 5:00 AM PST (which is equivalent to 6:00 AM PDT). Is there a way to force it to use another daylight savings mode?

Follow up: By the way, this is the Windows XP behavior. A file created on June 1, 6 AM will be seen (by Explorer, Command Prompt, etc) as June 1, 5 AM during the winter.

+1  A: 

I don't think there's a way to force the DateFormat to apply a TimeZone format to a time that couldn't exist. PST is only applicable in the Winter months and PDT is only used in the summer.

I formatted this date with all of the TimeZones returned by TimeZone.getAvailableIDs(), and found two that returned me PST for that particular time: Pacific/Pitcairn and SystemV/PST8. You could try using one of those, but I have no idea what other rules apply to either of those timezone objects.

If you're just concerned with getting the time with that offset, you could use GMT-8 as your TimeZone. However, that string will show up in your formatted date string instead of PST.

UPDATE: Here's a way to take any timezone and have it ignore all daylight savings time rules. You can grab a TimeZone object then grab another TimeZone.getTimeZone() object. On the second object set the Id and rawOffset to the corresponding object from the first TimeZone.

For example:

 DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm aa zzz");
 TimeZone defaultTimeZone = TimeZone.getDefault();
 TimeZone timeZone = TimeZone.getTimeZone("");
 timeZone.setID(defaultTimeZone.getID());
 timeZone.setRawOffset(defaultTimeZone.getRawOffset());
 dateFormat.setTimeZone(timeZone);
 System.out.println(dateFormat.format(new Date(1243861200000L)));

This will print out exactly what you're looking for.

Jason Gritman
+1  A: 

Hi Steve, this might do the trick:

 /*
  * Create a date that represents 1,243,861,200,000 milliseconds since
  * Jan 1, 1970
  */
 Date date = new Date(1243861200000L);

 /*
  * Print out the date using the current system's default format and time
  * zone. In other words, this will print differently if you set the
  * operating zone's time zone differently.
  */
 System.out.println(date);

 /*
  * Specify the format and timezone to use
  */
 DateFormat dateFormat = new SimpleDateFormat(
   "MMM dd, yyyy hh:mm aa zzz");
 TimeZone pstTZ = TimeZone.getTimeZone("PST");
 dateFormat.setTimeZone(pstTZ);
 System.out.println(dateFormat.format(date));

 /*
  * It looks like what you want is to actually display a different date
  * (different # milliseconds since epoch) in the summer months. To do that, you
  * need to change the date by subtracting an hour.
  */
 if(pstTZ.inDaylightTime(date)){
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  cal.add(Calendar.HOUR, -1);
  date = cal.getTime();
 }
 //now this should always print "5:00". However, it will show as 5:00 PDT in summer and 5:00 PST in the winter which are actually 2 different dates. So, I think you might need to think about exactly what it is you're trying to achieve. 
 System.out.println(dateFormat.format(date));
Dave Paroulek
-1 The date will then be incorrect during the winter months, when the date is really in PST.
Jason Gritman
Thanks, Jason, I see how that's misleading, I'll update my answer to be more clear about what I was trying to show by subtracting an hour from the date.
Dave Paroulek
+1  A: 

PST/PDT change throughout the year. Java attempts to determine which one is in affect on a given date. This things obviously change as the laws and regions change. Can you use an absolute time zone? Specifying the offset in GMT will be the same year around.

 DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm aa zzz");
 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT-07:00"));
 System.out.println(dateFormat.format(new Date(1243861200000L)));

Jun 01, 2009 06:00 AM GMT-07:00
brianegge