JDK-6902861 : (cal) GregorianCalendar roll WEEK_OF_YEAR is broken for January 1 2010
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 7
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2009-11-19
  • Updated: 2013-10-17
  • Resolved: 2013-10-02
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.
JDK 8
8 b112Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b52)
Java HotSpot(TM) Client VM (build 15.0-b03, mixed mode, sharing)

but also happens for latest 1.6

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600] (but happens for all versions)


A DESCRIPTION OF THE PROBLEM :
I've read previous bug reports and it seems like the complication is caused by roll() not changing lower valued fields, but I think it is impossible to justify the current behaviour. If you roll 1 Jan 2010 by 1 WEEK_OF_YEAR you get the WEEK_OF_YEAR moving from 53 to 4 (which isn't a move of 1 by any definition) and the date is 29 January 2010.

I guess Sun wish they'd never written the Spec of roll() as it stands, but I think the current behaviour cannot be justified as meeting the spec.

Test code below.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Test code below.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I'd expect 8th January 2010, or if it is assuming this is the 53rd week of 2009, then a day in the first week of 2009.



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;

public static void main(String [] args) {
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.YEAR, 2010);
    int week = calendar.get(Calendar.WEEK_OF_YEAR);
    System.out.println("week = " + week);
    calendar.roll(Calendar.WEEK_OF_YEAR, 1);
    int week2 = calendar.get(Calendar.WEEK_OF_YEAR);
    System.out.println("week2 = " + week2);
    System.out.println("day = " + calendar.get(Calendar.DAY_OF_MONTH));
    System.out.println("month = " + calendar.get(Calendar.MONTH));
    System.out.println("year = " + calendar.get(Calendar.YEAR));
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Haven't found one yet, but add would probably work.

Comments
WEEK_OF_YEAR of the Calendar API has problems (JDK-4267450) and some week-based year support was added in JDK 7. The symptom is due to combination of the ISO-style week numbering and calendar year. The workaround is to try staying in the calendar year even with out of bounds week conditions.
26-09-2013