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()