SimpleDateFormat's parse method returns a different date from the date that is stored in the object afterwards. This is demonstrated in the following code:
DateFormat sdf = new SimpleDateFormat("M/d/yy");
DateFormat output = new SimpleDateFormat("M/d/yyyy");
{
String s = "1/1/18";
Date d = sdf.parse(s);
Calendar c = sdf.getCalendar();
System.out.println("Parsing "+s+" to get Date and Calendar objects");
System.out.println("date = "+output.format(d));
System.out.println("calendar = "+output.format(c.getTime()));
}
System.out.println("");
{
String s = "12/31/18";
System.out.println("Parsing "+s+" to get Date and Calendar objects");
Date d = sdf.parse(s);
Calendar c = sdf.getCalendar();
System.out.println("date = "+output.format(d));
System.out.println("calendar = "+output.format(c.getTime()));
}
The Calendar value retrieved from SimpleDateFormat.getCalendar() is based on the year only, whereas the Date value returned from SimpleDateFormat.parse() is based on the instant when the object was created.
The bug exists in all investigated versions of JDK from 1.1.5 to 1.2.
See http://javaweb.israel/javapc_qa/Y2K/ComplianceNotes.html for more details.
DateFormat sdf = new SimpleDateFormat("M/d/yy");
DateFormat output = new SimpleDateFormat("M/d/yyyy");
{
String s = "1/1/18";
Date d = sdf.parse(s);
Calendar c = sdf.getCalendar();
System.out.println("Parsing "+s+" to get Date and Calendar objects");
System.out.println("date = "+output.format(d));
System.out.println("calendar = "+output.format(c.getTime()));
}
System.out.println("");
{
String s = "12/31/18";
System.out.println("Parsing "+s+" to get Date and Calendar objects");
Date d = sdf.parse(s);
Calendar c = sdf.getCalendar();
System.out.println("date = "+output.format(d));
System.out.println("calendar = "+output.format(c.getTime()));
}
The Calendar value retrieved from SimpleDateFormat.getCalendar() is based on the year only, whereas the Date value returned from SimpleDateFormat.parse() is based on the instant when the object was created.
The bug exists in all investigated versions of JDK from 1.1.5 to 1.2.
See http://javaweb.israel/javapc_qa/Y2K/ComplianceNotes.html for more details.