JDK-4197699 : Incorrect WEEK_OF_YEAR for Gregorian Calendar
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.1.8,1.2.0
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 1998-12-15
  • Updated: 1999-11-24
  • Resolved: 1999-01-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other Other Other Other Other
1.1.6_007 007Fixed 1.1.7Fixed 1.1.8Fixed 1.2.1_003Fixed 1.2.2Fixed
Related Reports
Relates :  
Description
The date is initially set to 31 Dec 2000.  The year is then rolled to 2001.  The WEEK_OF_YEAR should then be 53 (at least according to the behaviour of 1.1.7).  For 1.1.8, WEEK_OF_YEAR=1.

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

public class RollYear {

    RollYear() {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setLenient(true);
        cal.setFirstDayOfWeek(2); // Monday
        cal.setMinimalDaysInFirstWeek(4);
        cal.set(2000, 11, 31);
        System.out.print("Initial date : ");
        displayDate(cal);
        cal.roll(cal.YEAR, true); // roll up year by 1
        System.out.print("Rolled to    : ");
        displayDate(cal);
        // WEEK_OF_YEAR should now be 53
        int woy = cal.get(cal.WEEK_OF_YEAR);
        if (woy != 53) {
            throw new RuntimeException("Test Failed: WEEK_OF_YEAR should now be
 53, not " + woy);
        }
        System.out.print("Test Passed");
    }

    void displayDate(GregorianCalendar gc) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("dd MMM yyyy");
        Date displayDate = gc.getTime();
        System.out.println(sdf.format(displayDate));
    }

    public static void main(String args[]) {
        new RollYear();
    }

}

stuart.lawrence@eng 1998-12-15

There is also a problem when the initial date is 01 Jan 1999 (replace above cal.set(2000, 11, 31) with cal.set(1999, 0, 1)).  In this case, after the roll, WEEK_OF_YEAR=0 for 1.1.7 and WEEK_OF_YEAR=53 for 1.1.8 (build C).

stuart.lawrence@eng 1998-12-16

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.1.8 1.2.2 FIXED IN: 1.1.6_007 1.1.7b_005 1.1.8 1.2.1-003 1.2.2 INTEGRATED IN: 1.1.6_007 1.1.7b_005 1.1.8 1.2.1-003 1.2.2 VERIFIED IN: 1.1.8
14-06-2004

EVALUATION As this looks like a regression, I am commiting it to 1.1.8. stuart.lawrence@eng 1998-12-16 On further examination, both 1.1.7 and 1.1.8/1.2 are wrong! Here is a test cases: With first day of week MONDAY, and minimal days in first week 4: 1 Jan 2000 should get: 52 1.1.8d/1.2: 53 The suggested fix is a low-risk, restricted change to two lines of code. alan.liu@eng 1998-12-18 The bug is verified as fixed in 118f via regression testing. However, the regression tests errors under Javatest 2.0, the test is out of spec and to be re-written. tao.zhang@eng 1999-01-25 fumihiko.iwabuchi@japan 1999-03-01 JS asked the bug verification to LCs. This bug is NOT Fixed in JDK1.1.8 build "H". (JLC) Ran the program in the description field on Solaris 2.6 ja. iwabuchi@dandelion> java -fullversion java full version "JDK1.1.8H" iwabuchi@dandelion> java RollYear Initial date : 31 12 2000 Rolled to : 31 12 2001 java.lang.RuntimeException: Test Failed: WEEK_OF_YEAR should now be 53, not 1 at RollYear.<init>(RollYear.java:20) at RollYear.main(RollYear.java:33) iwabuchi@dandelion> java -fullversion java full version "JDK1.1.7R" iwabuchi@dandelion> java RollYear Initial date : 31 12 2000 Rolled to : 31 12 2001 Test Passed iwabuchi@dandelion> java -fullversion java full version "JDK1.1.6N" iwabuchi@dandelion> java RollYear Initial date : 31 12 2000 Rolled to : 31 12 2001 Test Passed --- The problem being seen in Japan has nothing to do with the Japanese locale. The problem is that the test in the description ("RollYear.java") is wrong. It thinks that Dec 31 2001 (with Monday as first day of week, and 4 days in first week) is week 53; it's not -- it's week 1 of 2002. Don't use RollYear; use test/java/util/Calendar/CalendarRegression.java, test method Test4197699. alan.liu@eng 1999-03-04
04-03-1999

SUGGESTED FIX In GregorianCalendar.java: *** 495,505 **** else if (woy == 0) { // We are the last week of the previous year. int prevDoy = dayOfYear + yearLength(rawYear - 1); ! int prevDow = (dayOfWeek + 6) % 7; // 0..6; This is actually DOW-1 % 7 ! // The following line is unnecessary because weekNumber() will ! // do any needed normalization internally. ! // if (prevDow == 0) prevDow = 7; // 1..7 ! woy = weekNumber(prevDoy, prevDow); } internalSet(WEEK_OF_YEAR, woy); --- 494,500 ---- else if (woy == 0) { // We are the last week of the previous year. int prevDoy = dayOfYear + yearLength(rawYear - 1); ! woy = weekNumber(prevDoy, dayOfWeek); } internalSet(WEEK_OF_YEAR, woy); alan.liu@eng 1998-12-18
18-12-1998