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

java.util.GregorianCalendar.getActualMaximum(WEEK_OF_MONTH) works wrong

XMLWordPrintable

    • 1.2fcs
    • sparc
    • solaris_2.5
    • Verified



      Name: dfC67450 Date: 08/13/98



      java.util.GregorianCalendar.getActualMaximum(WEEK_OF_MONTH) ignores setting of
      firstDayOfWeek. getActualMaximum should take into account this setting.

      Here is the test demonstrating the bug:

      -----------------Test.java------------------------
      import java.util.*;

      public class Test {
        public static void main (String args[]){


          boolean passed = true;
          int field = Calendar.WEEK_OF_MONTH;

          GregorianCalendar calendar = new GregorianCalendar(1998, 2, 1);
          Date date = calendar.getTime();
          int firstInMonth = calendar.get(Calendar.DAY_OF_MONTH);
          System.out.println("Date: " + date);

          for (int firstInWeek = 1; firstInWeek <= 7; firstInWeek++) {
            calendar.setFirstDayOfWeek(firstInWeek); // Sunday
            int maxWeekOfMont = calendar.getActualMaximum(field);
            int returned = calendar.getActualMaximum(field);
            int expected = (31 + ((firstInMonth - firstInWeek + 7)% 7) + 6) / 7;

            System.out.println(" FirstDayOfWeek: " + firstInWeek );
            System.out.println(" getActualMaximum(WEEK_OF_MONTH) = " + returned );
            System.out.println(" expected = " + expected);
            if (returned != expected) {
              passed = false;
              System.out.println(" !!! bug !!!");
            }
            System.out.println();
          }
          if (passed) System.out.println(" Test passed" );
          else System.out.println(" Test failed" );
        }

      }
      ---------Output from the test---------------------
      Date: Sun Mar 01 00:00:00 GMT+03:00 1998
        FirstDayOfWeek: 1
        getActualMaximum(WEEK_OF_MONTH) = 5
        expected = 5

        FirstDayOfWeek: 2
        getActualMaximum(WEEK_OF_MONTH) = 4
        expected = 6
        !!! bug !!!

        FirstDayOfWeek: 3
        getActualMaximum(WEEK_OF_MONTH) = 4
        expected = 6
        !!! bug !!!

        FirstDayOfWeek: 4
        getActualMaximum(WEEK_OF_MONTH) = 4
        expected = 5
        !!! bug !!!

        FirstDayOfWeek: 5
        getActualMaximum(WEEK_OF_MONTH) = 4
        expected = 5
        !!! bug !!!

        FirstDayOfWeek: 6
        getActualMaximum(WEEK_OF_MONTH) = 4
        expected = 5
        !!! bug !!!

        FirstDayOfWeek: 7
        getActualMaximum(WEEK_OF_MONTH) = 4
        expected = 5
        !!! bug !!!

        Test failed
      -------------------------------------------------

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

            aliusunw Alan Liu (Inactive)
            dfazunensunw Dmitri Fazunenko (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: