Bug 29690 - GregorianCalendar implementation disagrees with itself
Summary: GregorianCalendar implementation disagrees with itself
Status: UNCONFIRMED
Alias: None
Product: classpath
Classification: Unclassified
Component: classpath (show other bugs)
Version: 0.92
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-11-02 18:39 UTC by Tamir Nitzan
Modified: 2006-11-02 18:39 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tamir Nitzan 2006-11-02 18:39:45 UTC
Symptom: get an IllegalArgumentException when performing this sequence:
/**** symptom ****/
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setLenient(false);
            calendar.set(java.util.Calendar.YEAR, 2001);
            calendar.set(java.util.Calendar.MONTH, 11);
            calendar.set(java.util.Calendar.DATE, 30);
            calendar.clear(java.util.Calendar.DST_OFFSET);
            calendar.clear(java.util.Calendar.ZONE_OFFSET);

            java.util.Date d = calendar.getTime();
/**** end symptom ****/

Note that Dec. 31, 2001 is a valid date.

OK, I confess to not understanding all the code in this implementation (some of the algorithms are a little cryptic), but I did find this out:

/**** snippet from computeTime() ****/
/* this snippet ends up setting the WEEK_OF_YEAR to 53 */

    int weekOfYear = (fields[DAY_OF_YEAR] - relativeWeekday + 6) / 7;

    // Do the Correction: getMinimalDaysInFirstWeek() is always in the 
    // first week.
    int minDays = getMinimalDaysInFirstWeek();
    int firstWeekday = (7 + getWeekDay(fields[YEAR], minDays)
                       - getFirstDayOfWeek()) % 7;
    if (minDays - firstWeekday < 1)
        weekOfYear++;
    fields[WEEK_OF_YEAR] = weekOfYear;
/**** end snippet from computeTime() ****/

whereas,
/**** snippet from nonLeniencyCheck() ****/
/* this snippet comes back saying there were only 52 weeks in 2001!! */

        int daysInYear = 365 + leap;
        daysInYear += (getFirstDayOfMonth(year, 0) - 1); // pad first week
        int last = getFirstDayOfMonth(year, 11) + 4;
        if (last > 7)
            last -= 7;
        daysInYear += 7 - last;
        int weeks = daysInYear / 7;
        if (fields[WEEK_OF_YEAR] < 1 || fields[WEEK_OF_YEAR] > weeks)
            throw new IllegalArgumentException("Illegal WEEK_OF_YEAR.");
/**** end snippet from nonLeniencyCheck() ****/

It seems something is wrong in one of these calculations, but honestly this is so abstract I can't figure out which one is correct :)

Thanks for the help,
   Tamir Nitzan