Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6609376

(cal) setting Calendar HOUR_OF_DAY to zero does not work for non-default TimeZones

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.5.0_07"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
      Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      WIndows XP Pro Service Pack 2

      A DESCRIPTION OF THE PROBLEM :
      setting HOUR_OF_DAY to zero using Calendar's set(Calendar.HOUR_OF_DAY,0) method only works as expected for a calendar using default TimeZone.

      For other non-default TimeZones, the hour field is NOT set to zero; rather it is set to a number predictably dependent on the TimeZone being used.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      see included source code - nothing fancy


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      expected hour to be set to zero regardless of calendar's timezone

      ACTUAL -
      Demonstrate set Calendar HOUR_OF_DAY bug:
         Expected: HOUR_OF_DAY = 0
         Results: HOUR_OF_DAY = non-zero (predictably dependent on Calendar's TimeZone)
      Calendar.TimeZone(Default)=GMT-05:00
      Calendar.TimeZone(CST) =GMT-06:00
      Calendar.TimeZone(PST) =GMT-08:00
      Date=01/02/03 04:05:06 [original date as a String]
      Date=01/02/03 04:05:06 [Date parsed from string]
      Date=01/02/03 04:05:06 [Calendar-Default]
      Date=01/02/03 04:05:06 [Calendar-CST]
      Date=01/02/03 04:05:06 [Calendar-PST]
      Now set HOUR_OF_DAY to zero using cal.set(Calendar.HOUR_OF_DAY,0):
      Date=01/02/03 00:05:06 [Calendar-Default][Hour=0]
      Date=01/02/03 01:05:06 [Calendar-CST ][Hour=0] <---BUG?
      Date=01/02/03 03:05:06 [Calendar-PST ][Hour=0] <---BUG?
      ----- HOUR_OF_DAY not as expected -----


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * CalendarSetBug.java
       *
       * Created on July 20, 2006, 10:00 AM
       *
       */

      package calendarbug;
      import java.util.*;
      import java.text.*;

      /**
       *
       * Demonstrate bug in java.util.Calendar
       * Setting HOUR_OF_DAY to zero yields non-zero results
       * for non-default TimeZones.
       *
       */
      public class CalendarSetBug {
          Calendar cal; // calendar default timezone
          Calendar calCST; // calendar CST timezone
          Calendar calPST; // calendar PST timezone
          String sdt; // initial datetime string
          Date dt; // datetime
          DateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
          
          public CalendarSetBug() {
              System.out.printf("Demonstrate set Calendar HOUR_OF_DAY bug:\r\n");
              System.out.printf(" Expected: HOUR_OF_DAY = 0\r\n");
              System.out.printf(" Results: HOUR_OF_DAY = non-zero (predictably dependent on Calendar's TimeZone)\r\n");

              cal = Calendar.getInstance(TimeZone.getDefault());
              calCST = Calendar.getInstance(TimeZone.getTimeZone("GMT-06:00"));
              calPST = Calendar.getInstance(TimeZone.getTimeZone("GMT-08:00"));

              System.out.printf("Calendar.TimeZone(Default)=%s\r\n",cal.getTimeZone().getDisplayName());
              System.out.printf("Calendar.TimeZone(CST) =%s\r\n",calCST.getTimeZone().getDisplayName());
              System.out.printf("Calendar.TimeZone(PST) =%s\r\n",calPST.getTimeZone().getDisplayName());

              try {
                  sdt="01/02/03 04:05:06";
                  dt = df.parse(sdt);
                  cal.setTime(dt);
                  calCST.setTime(dt);
                  calPST.setTime(dt);

                  System.out.printf("Date=%s [original date as a String]\r\n",sdt);
                  System.out.printf("Date=%s [Date parsed from string]\r\n",df.format(dt));
                  System.out.printf("Date=%s [Calendar-Default]\r\n",df.format(cal.getTime()));
                  System.out.printf("Date=%s [Calendar-CST]\r\n",df.format(calCST.getTime()));
                  System.out.printf("Date=%s [Calendar-PST]\r\n",df.format(calPST.getTime()));

                  System.out.printf("Now set HOUR_OF_DAY to zero using cal.set(Calendar.HOUR_OF_DAY,0):\r\n");

                  cal.set(Calendar.HOUR_OF_DAY,0);
                  System.out.printf("Date=%s [Calendar-Default][Hour=0]\r\n",df.format(cal.getTime()));

                  calCST.set(Calendar.HOUR_OF_DAY,0);
                  System.out.printf("Date=%s [Calendar-CST ][Hour=0] <---BUG?\r\n",df.format(calCST.getTime()));

                  calPST.set(Calendar.HOUR_OF_DAY,0);
                  System.out.printf("Date=%s [Calendar-PST ][Hour=0] <---BUG?\r\n",df.format(calPST.getTime()));
                  System.out.printf("----- HOUR_OF_DAY not as expected -----\r\n\r\n");
                  }
              catch (Exception e) {
              }
          }
          
          public static void main(String[] args) {
              CalendarSetBug csb = new CalendarSetBug();
          }
          
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      suppose i could workaround if i need to, but seems a shame.

      did play around with suggested intermixed calls to calendar's getTime() -- didn't change results for me


      don't see anywhere else to post this so...many things i really like about using java, my hat's off to you guys, just surprised to see Calendar/Date stuff so cumbersome and at least for my use at the moment, buggy.

            Unassigned Unassigned
            okutsu Masayoshi Okutsu
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: