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

(cal) Calendar returns bad values if a first get is not call before a set of a weekday

XMLWordPrintable

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

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 2000
      Service Pack 4

      A DESCRIPTION OF THE PROBLEM :
      When trying to display dates, there is a problem if we don't do a get before a set of a weekday of Calendar instance. It returns the first week of the month instead of the one selected by the day.
      By executing this code in debug mode, everything seems to work well.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the code in test case with and without the comment


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      2005/5/15
      2005/5/16
      2005/5/17
      2005/5/18
      2005/5/19
      2005/5/20
      2005/5/21

      ACTUAL -
      2005/5/29
      2005/5/30
      2005/5/31
      2005/6/1
      2005/6/2
      2005/6/3
      2005/6/4


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * Created on 2005-05-30
       */
      import java.util.Calendar;

      /**
       * @author Jean-Sebastien Goupil
       */
      public class Testing {
      public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.DAY_OF_MONTH, 20);
      cal.set(Calendar.MONTH, 4);
      cal.set(Calendar.YEAR, 2005);

      // Uncomment this next line to have a correct behaviour
      //cal.get(Calendar.DAY_OF_MONTH);
      cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
      for (int i = 0; i < 7; i++) {
      int day = cal.get(Calendar.DAY_OF_MONTH);
      int month = cal.get(Calendar.MONTH) + 1;
      int year = cal.get(Calendar.YEAR);
      System.out.println(year + "/" + month + "/" + day);
      cal.add(Calendar.DAY_OF_MONTH, 1);
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Uncomment the following line :
      //cal.get(Calendar.DAY_OF_MONTH);
      and it seems to work for now.
      ###@###.### 2005-06-22 09:02:02 GMT

            okutsu Masayoshi Okutsu
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: