JDK-6609452 : [Fmt-Da] DateFormat.getCalendar().getTime() outputs wrong year
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.text
  • Affected Version: 1.4.2
  • Priority: P5
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000,windows_xp
  • CPU: x86
  • Submitted: 2007-09-26
  • Updated: 2010-07-29
  • Resolved: 2007-10-19
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.4.2_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_06-b03)
Java HotSpot(TM) Client VM (build 1.4.2_06-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
MS Windows XP 5.1.2600
Linux 2.4.20-8 i386

EXTRA RELEVANT SYSTEM CONFIGURATION :
Standard

A DESCRIPTION OF THE PROBLEM :
The following code:

	DateFormat myDateFormat= DateFormat.getInstance();
	Calendar myCalendar= myDateFormat.getCalendar();
	Date dateCurrDate= myCalendar.getTime();
	String strCurrDate= myDateFormat.format(dateCurrDate);

gives a shift in time 80 years back, for instance, if it is 12/2/04 12:32, the strCurrDate string contains "12/2/24 12:32" (the long version would be "Dec 2, 1924 12:32:14 PM"). The rest of the date/time string (except the year) is correct.

If the Calendar instance is obtained using Calendar.getInstance(), or Date is created by new Date(), the date output is correct. The following two code examples provide a correct date/time output:

1. Code example (Calendar.getInstance() is used):

	DateFormat myDateFormat= DateFormat.getInstance();
	Calendar myCalendar= Calendar.getInstance();
	Date dateCurrDate= myCalendar.getTime();
	String strCurrDate= myDateFormat.format(dateCurrDate);

2. Code example (new Date() is used):

	DateFormat myDateFormat= DateFormat.getInstance();
	Date dateCurrDate= new Date();
	String strCurrDate= myDateFormat.format(dateCurrDate);


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run this code:

	DateFormat myDateFormat= DateFormat.getInstance();
	Calendar myCalendar= myDateFormat.getCalendar();
	Date dateCurrDate= myCalendar.getTime();
	String strCurrDate= myDateFormat.format(dateCurrDate);

and inspect strCurrDate

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
"mm/dd/abs[yy-80] hh:mm [AM|PM]"
ACTUAL -
"mm/dd/yy hh:mm [AM|PM]"

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
	DateFormat myDateFormat= DateFormat.getInstance();
	Calendar myCalendar= myDateFormat.getCalendar();
	Date dateCurrDate= myCalendar.getTime();
	String strCurrDate= myDateFormat.format(dateCurrDate);

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

CUSTOMER SUBMITTED WORKAROUND :
Use Calendar.getInstance(), or new Date()

Comments
EVALUATION The Calendar instance in a DateFormat is first used to calculate the base year for the 2-digit year handling. Please don't rely on the Calendar fields.
19-10-2007