views:

236

answers:

2
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?

+1  A: 

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

cherouvim
I know that it works... But if i pass 07312011, it gives some random date in 1.4 and null in 1.5So, 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?
java_geek
If you know that it works then you should mention that in the question and explicitly state that you are interested in JDK inconsistencies for API method calls with bad data.
cherouvim
+1  A: 

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.

josefx