JDK-6278731 : java.util.Calendar.getYear() wrong
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-06-01
  • Updated: 2010-07-29
  • Resolved: 2005-06-02
Description
FULL PRODUCT VERSION :
java version "1.4.2_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_07-b05)
Java HotSpot(TM) Client VM (build 1.4.2_07-b05, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
import java.util.*;
public class Test
{
	public static void main(String[] args)
	{
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, -1000);
		System.out.println(c.toString());
		System.out.println(c.get(Calendar.YEAR));
		System.out.println(c.getTime());
		System.out.println(c.toString());
	}
}

output:
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.uti
l.calendar.ZoneInfo[id="Asia/Taipei",offset=28800000,dstSavings=0,useDaylight=false,transitions=42,l
astRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=-1000,MONTH=5,WEEK_OF_YEAR=23,WEE
K_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=152,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=6,HOUR
_OF_DAY=18,MINUTE=41,SECOND=51,MILLISECOND=637,ZONE_OFFSET=28800000,DST_OFFSET=0]
1001
Fri Jun 01 18:41:51 CST 1001
java.util.GregorianCalendar[time=-93711820688363,areFieldsSet=true,areAllFieldsSet=true,lenient=true
,zone=sun.util.calendar.ZoneInfo[id="Asia/Taipei",offset=28800000,dstSavings=0,useDaylight=false,tra
nsitions=42,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=0,YEAR=1001,MONTH=5,WEEK_OF
_YEAR=22,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=153,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1
,HOUR=6,HOUR_OF_DAY=18,MINUTE=41,SECOND=51,MILLISECOND=637,ZONE_OFFSET=28800000,DST_OFFSET=0]


System.out.println(c.toString());  ==> YEAR=-1000
System.out.println(c.get(Calendar.YEAR)); ==>1001
System.out.println(c.getTime()); ==>Fri Jun 01 18:37:25 CST 1001
System.out.println(c.toString());  ==> YEAR=1001

the c.toString() prints the right year,  -1000, but c.get(Calendar.YEAR) and c.getTime() is wrong, the year become 1001, print again the calendar, the year wrong again.

it must a bug in java.util.Calendar.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
just compile and run the source code.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
System.out.println(c.toString());  ==> YEAR=-1000
System.out.println(c.get(Calendar.YEAR)); ==>-1000
System.out.println(c.getTime()); ==>Fri Jun 01 18:37:25 CST -1000
System.out.println(c.toString());  ==> YEAR=-1000
ACTUAL -
System.out.println(c.toString());  ==> YEAR=-1000
System.out.println(c.get(Calendar.YEAR)); ==>1001
System.out.println(c.getTime()); ==>Fri Jun 01 18:37:25 CST 1001
System.out.println(c.toString());  ==> YEAR=1001


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;
public class Test
{
	public static void main(String[] args)
	{
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, -1000);
		System.out.println(c.toString());
		System.out.println(c.get(Calendar.YEAR));
		System.out.println(c.getTime());
		System.out.println(c.toString());
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
NO WAY TO WORKAROUND
###@###.### 2005-06-01 12:16:43 GMT

Comments
EVALUATION GregorianCalendar represents a date with ERA and YEAR. 0 and negative year values are converted to (1 - year) with an ERA change to support the Julian calendar year numbering. ###@###.### 2005-06-02 07:04:10 GMT
2005-06-02