6 Replies Latest reply on Apr 11, 2007 10:35 PM by jschellSomeoneStoleMyAlias

    SimpleDateFormat / Daylight Savings time issue in 1.5.0_11 ?

    807606
      I am having trouble with SimpleDateFormat.format(), possibly due to Daylight Savings Time, but I can't figure it out.
            import java.text.*;
            import java.util.*;
            
            public class Test {
              public static void main(String[] args) throws Exception{
                SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd z yyyy");
                Date date = dateFormat.parse("Tue April 13 CDT 2007");
                System.out.println(dateFormat.format(date)); 
                
                System.out.println(date.toString());
                
                SimpleDateFormat dateFormat2 = new SimpleDateFormat("MMMM dd, yyyy");
                System.out.println(dateFormat2.format(date));
              }
            }
      I am in the CDT time zone.

      If I run this code on JRE 1.5.0_04-b05, I get:
      Fri Apr 13 CDT 2007
      Fri Apr 13 00:00:00 CDT 2007
      April 13, 2007
      This is what I expect. But, if I run this code on JRE 1.5.0_11-b03, I get:
       
      Fri Apr 13 CDT 2007
      Thu Apr 12 23:00:00 GMT-06:00 2007   
      April 12, 2007
      I don't understand why, the time calculated to be an hour off, since my time zone and locale are the same as that of the input string?

      I thought this could be a DST issue, but a) both dates are in DST, and b) I tried applying the fix to the Java 5 DST bug, on the off chance it would help. but it did nothing. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6530336

      I'm stumped. Can anyone help?
      Thanks,
      Ed
        • 1. Re: SimpleDateFormat / Daylight Savings time issue in 1.5.0_11 ?
          DrClap
          I believe that they have finally got serious about the deprecation of obsolete timezone abbreviations like "EST" that happened when Java 2 was released several years ago.
          • 2. Re: SimpleDateFormat / Daylight Savings time issue in 1.5.0_11 ?
            796365
            dateFormat.parse("Tue April 13 CDT 2007");
            The results are due to invalid input data. April 13, 2007 is a Friday, not Tuesday.
            • 3. Re: SimpleDateFormat / Daylight Savings time issue in 1.5.0_11 ?
              807606
              No, the day of the week doesn't matter:
              import java.text.*;
              import java.util.*;
              
              public class Test {
                public static void main(String[] args) throws Exception{
                  SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd z yyyy");
                  SimpleDateFormat dateFormat2 = new SimpleDateFormat("MMM dd, yyyy");
                  for (String day: Arrays.asList("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")){
                    Date date = dateFormat.parse(day + " Apr 13 CDT 2007");  
                    System.out.println(day + " " + dateFormat2.format(date) + " " + date.getTime());
                  }
                }
              }
              outputs:
              Sun Apr 12, 2007 1176440400000
              Mon Apr 12, 2007 1176440400000
              Tue Apr 12, 2007 1176440400000
              Wed Apr 12, 2007 1176440400000
              Thu Apr 12, 2007 1176440400000
              Fri Apr 12, 2007 1176440400000
              Sat Apr 12, 2007 1176440400000
              • 4. Re: SimpleDateFormat / Daylight Savings time issue in 1.5.0_11 ?
                807606
                I believe that they have finally got serious about
                the deprecation of obsolete timezone abbreviations
                like "EST" that happened when Java 2 was released
                several years ago.
                No, even with the full timezone name, it doesn't behave as expected:
                import java.text.*;
                import java.util.*;
                
                public class Test {
                  public static void main(String[] args) throws Exception{
                    SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd z yyyy");
                    SimpleDateFormat dateFormat2 = new SimpleDateFormat("MMM dd, yyyy");
                    Date date = dateFormat.parse("Apr 13 Central Daylight Time 2007");  
                    System.out.println(date.toString());
                    System.out.println(dateFormat2.format(date));
                  }
                }
                Thu Apr 12 23:00:00 GMT-06:00 2007
                Apr 12, 2007
                • 5. Re: SimpleDateFormat / Daylight Savings time issue in 1.5.0_11 ?
                  807606
                  It looks like the problem is that Java doesn't have my timezone correct after all.
                  SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd z yyyy");    
                  System.out.println(dateFormat.getTimeZone());    
                  prints:
                  sun.util.calendar.ZoneInfo[id="GMT-06:00",offset=-21600000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]

                  ...which is wrong. It should be dstSavings=3600000,useDaylight=true.

                  So, one SimpleDateFormatter has CDT as its timezone, and the other has CST as its timezone. This changes the time from midnight on one day to 11 pm on the previous day, causing the wrong date to be shown.

                  This workaround gives the date I want, the original date at its (not my) timezone.
                      SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd z yyyy");    
                      Date date = dateFormat.parse("Tue April 13 CDT 2007");
                      SimpleDateFormat dateFormat2 = new SimpleDateFormat("MMMM dd, yyyy");
                      dateFormat2.setTimeZone(dateFormat.getTimeZone());
                      System.out.println(dateFormat2.format(date));
                  Now if I can coerce Java to give me my correct local time zone, I'll be set.
                  • 6. Re: SimpleDateFormat / Daylight Savings time issue in 1.5.0_11 ?
                    jschellSomeoneStoleMyAlias
                    Now if I can coerce Java to give me my correct local
                    time zone, I'll be set.
                    Java extracts that from the OS. So if it is 'wrong' then blame the OS.

                    Other than that you can set the default timezone in the application itself.