SimpleDateFormat returns incorrect date on jdk1.4

1.1k Views Asked by At
String format = "yyyyMMdd";
SimpleDateFormat formatter = getSimpleDateFormat(format);
formatter.setLenient(false);

Date date = formatter.parse("07312011",new ParsePosition(0));
System.out.println(date);

This gives "2500-01-01 00:00:00" on jdk1.4 which is incorrect and returns null on jdk1.5

Why does this give "2500-01-01 00:00:00" on jdk1.4? If it is not able to parse a date, does it default to this date?

UPDATE:

I know that if i use 20110731 it works... But if i pass 07312011, it gives some random date in 1.4 and null in 1.5 So, my questions are

In 1.4, if the passed date does does not correspond to the format, does it default to 2500-01-01?

Why does it return null on 1.5?

2

There are 2 best solutions below

2
On

Try passing it "20110731" which corresponds to the date format you are specifying.

0
On

Only a gues, there is a bug for java 1.4, for ignoring the setLenient(false) setting.
Java 1.5 does not have this bug and since there are no 20 months in a year fails to parse your input and returns null instead.