开发者

SimpleDateFormat returns incorrect date on jdk1.4

开发者 https://www.devze.com 2022-12-21 20:43 出处:网络
String format = \"yyyyMMdd\"; SimpleDateFormat formatter = getSimpleDateFormat(format); formatter.setLenient(false);
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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消