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

Setting Calendar week numbers doesn't always work

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.1
    • core-libs



      Name: bsC130419 Date: 06/29/2001


      java version "1.3.1"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
      Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

      Appended code illustrates the problem, which occurs when setFirstDayOfWeek()
      and setMinimalDaysInFirstWeek() are used. Actual and expected output
      noted in code comments.

      May be related to bug 4459653

      ==============================================================

      import java.util.*;

      public class Test {

          public static Calendar getDefaultCalendar() {
              Calendar cal = Calendar.getInstance();
              return cal;
          }
          
          public static Calendar getIsoCalendar() {
              Calendar cal = Calendar.getInstance();
              // ISO weeks start on a monday
              cal.setFirstDayOfWeek(Calendar.MONDAY);
              // The first ISO week of a year contains the first Thu of the year
              cal.setMinimalDaysInFirstWeek(4);
              return cal;
          }
          
          public static void main(String[] args) {
              
              System.out.println("\nUsing ISO calendar:");
              System.out.println("===================");
              
              Calendar cal1 = getIsoCalendar();
              cal1.set(2001, Calendar.AUGUST, 5);
              int w1 = cal1.get(Calendar.WEEK_OF_YEAR);
              System.out.println("Week number for Sun 5th Aug 2001: " + w1);
              // Above line correctly prints week "31"

              Calendar cal2 = getIsoCalendar();
              cal2.set(Calendar.YEAR, 2001);
              cal2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
              cal2.set(Calendar.WEEK_OF_YEAR, 31);
              System.out.println(" Date for Sun in week 31 of 2001: " + cal2.getTime().toString());
              // Above line should print week "Sun 5th Aug", no?
              // Actually it prints "Sun Jul 29", which is in week 30.
              
              System.out.println("\nUsing default calendar:");
              System.out.println("=======================");

              Calendar cal3 = getDefaultCalendar();
              cal3.set(2001, Calendar.AUGUST, 5);
              int w3 = cal3.get(Calendar.WEEK_OF_YEAR);
              System.out.println("Week number for Sun 5th Aug 2001: " + w3);
              // Prints week "32". Not sure what "default" means but let's play along.
              
              Calendar cal4 = getDefaultCalendar();
              cal4.set(Calendar.YEAR, 2001);
              cal4.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
              cal4.set(Calendar.WEEK_OF_YEAR, 32);
              System.out.println(" Date for Sun in week 32 of 2001: " + cal4.getTime().toString());
              // Prints "Sun 5th Aug 2001".
              // Unlike the ISO case, this is at least consistent.

          }
          
      }
      (Review ID: 127557)
      ======================================================================

            peytoia Yuka Kamiya (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: