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

(cal) Call to Calendar.get() affects the calendar date and becomes incorrect.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6, 7
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.6.0_16"
      Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
      Java HotSpot(TM) Client VM (build 14.2-b01, mixed mode, sharing)

      same issue happens with JDK5

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      Run the attach code and see the difference.
      Both parts are exactly the same except for the call : c1.get(Calendar.WEEK_OF_YEAR)

      Whatever a Java object is, Doing a get on it should NEVER affects its state.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the attached program

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The output is:
      Day 27
      Date 27/Dec/2009 12:00:00
      Day 27
      Date 27/Dec/2009 12:00:00


      ACTUAL -
      Day 31
      Date 31/Dec/2009 12:00:00
      Day 27
      Date 27/Dec/2009 12:00:00


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.util.Calendar;
      import java.text.*;
      import java.util.*;


      public class DateBug {
      private static SimpleDateFormat sdf = new SimpleDateFormat();
      private static FieldPosition fieldPos = new FieldPosition(0);
      private static String DEFAULT_DATE_TIME_PATTERN = "dd/MMM/yyyy hh:mm:ss";

      public DateBug(){
      }


      public static void main(String[] args) {
      Calendar c1 = Calendar.getInstance();
      c1.clear();
      c1.set(Calendar.YEAR, 2009);
      System.out.println(c1.get(Calendar.WEEK_OF_YEAR)); // <--- This statments affect the outcome.

      c1.set(Calendar.WEEK_OF_YEAR, 53);
      System.out.println("Day " + c1.get(Calendar.DAY_OF_MONTH));//<-- It Correct answer is 27 but it prints 31
      System.out.println("Date " + formatDateTime(c1.getTime(),DEFAULT_DATE_TIME_PATTERN));


      c1 = Calendar.getInstance();
      c1.clear();
      c1.set(Calendar.YEAR, 2009);
      // System.out.println(c1.get(Calendar.WEEK_OF_YEAR));

      c1.set(Calendar.WEEK_OF_YEAR, 53);
      System.out.println("Day " + c1.get(Calendar.DAY_OF_MONTH)); // <-- It Correct answer is 27
      System.out.println("Date " + formatDateTime(c1.getTime(),DEFAULT_DATE_TIME_PATTERN)); // Correct
      }

      public static String formatDateTime(Date date, String pattern) {
      StringBuffer buffer = new StringBuffer(20);
      try {
      if (date == null)
      return "";
      sdf.applyPattern(pattern);
      buffer = sdf.format(date, buffer, fieldPos);
      } catch (Exception ex) {
      buffer.append("Error");
      }

      return buffer.toString();
      }

      }
      ---------- END SOURCE ----------

            okutsu Masayoshi Okutsu
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: