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

Error calculating DAY_OF_WEEK_IN_MONTH in Calendar

XMLWordPrintable

    • beta
    • x86
    • windows_95, windows_nt



      Name: gsC80088 Date: 02/04/99


      There is a defect in the calculations of
      DAY_OF_WEEK_IN_MONTH in the Calendar object.
      This was apparently introduced in 1.1.6 (as
      1.1.5 worked correctly). When setting the
      Nth day (i.e. 3rd Friday, 2nd Tuesday), Calendar
      always returns the 1st of the month. Defect
      can be reproduced using the sample code on
      pages 250-251 of "The Java Class Libraries
      Second Edition, Volume 1", by Patrick Chan, et.
      al. published by Addison Wesley. Code is
      reproduced below:

      ---------
      import java.util.*;
      import java.text.*;

      class CalTest {
      // Prints the date of the n'th dayOfWeek in month, year.
      static void printDate(int n,int dayOfWeek,int month,int year) {
      DateFormat formatter = new SimpleDateFormat("MMM dd yyyy");
      Calendar cal = Calendar.getInstance();

      cal.clear();
      cal.set(Calendar.DAY_OF_WEEK_IN_MONTH,n);
      cal.set(Calendar.DAY_OF_WEEK,dayOfWeek);
      cal.set(Calendar.MONTH,month);
      cal.set(Calendar.YEAR,year);
      System.out.println(formatter.format(cal.getTime()));
      }

      public static void main(String[] args) {
      printDate(1,Calendar.SUNDAY,Calendar.JANUARY,1997); // Jan 05 1997
      printDate(4,Calendar.SUNDAY,Calendar.JANUARY,1997); // Jan 26 1997
      printDate(8,Calendar.SUNDAY,Calendar.JANUARY,1997); // Feb 23 1997

      printDate(-1,Calendar.SUNDAY,Calendar.JANUARY,1997); // Jan 26 1997
      printDate(-4,Calendar.SUNDAY,Calendar.JANUARY,1997); // Jan 05 1997
      printDate(-8,Calendar.SUNDAY,Calendar.JANUARY,1997); // Dec 08 1996
      }
      }
      ---------------
      Please note that this feature is very useful for
      calculating holidays for use with financial software.
      (Review ID: 40612)
      ======================================================================

            aliusunw Alan Liu (Inactive)
            gstone Greg Stone
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: