I am using Joda Time 2.3 to convert String to java.lang.Date.
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
Here is some test code:
System.out.println(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("1927-12-11 11:22:38"));
System.out.println(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("1927-12-11 11:22:38").toDate());
System.out.println(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("1937-12-11 11:22:38"));
System.out.println(DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").parseDateTime("1937-12-11 11:22:38").toDate());
It works well under JDK 5 and JDK 7. But when using JDK 6, the result is:
1927-12-11T11:22:38.000+08:05:57
Sun Dec 11 11:22:33 CST 1927 // lose 5 second
1937-12-11T11:22:38.000+08:00
Sat Dec 11 11:22:38 CST 1937
You see that the first convert to java.lang.Date lost 5 second, but the second is right. The only difference is the year number, which the first is 1927 and the second is 1937. All the year number below 1927 will cause the wrong.
This must be something wrong about joda. Can somebody tell me how to avoid the bug, or I have to use SimpleDateFormatter.
Thank you!