Skip to main content

Did anyone notice the bug with java.util.Date 1st may 1921 ?

4 replies [Last post]
mmakundi
Offline
Joined: 2007-08-14

Did anyone notice the following bug:

// This fails in my locale:
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
System.out.println(dateFormat.parse("19210501")); // Displays: Sun May 01 00:20:08 EET 1921

// And this fails too in my locale:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, Calendar.MAY);
calendar.set(Calendar.YEAR, 1921);
System.out.println(calendar.getTime()); // Displays: Sun May 01 00:20:08 EET 1921

I.e., the timepart does not agree to be zero.

Any comments? Work-arounds?

It is pretty itchy when I use this date to store it into a database using Hibernate. Luckily we are using triggers to assert the timeparts are zero.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
spdenne
Offline
Joined: 2006-02-15

Could it be a timezone conversion problem?
It looks similar to bug id 6281408, in which case a workaround is to set the timezone to "GMT+02:00" or "GMT+03:00"

mmakundi
Offline
Joined: 2007-08-14

Nope. There result is simply rotated but still wrong:

Sat Apr 30 23:39:52 EET 1921
Sat Apr 30 23:39:52 EET 1921

Using:

// This fails:
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+2")); // NEW TIMEZONE
System.out.println(dateFormat.parse("19210501"));

// And this fails too:
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT+2")); // NEW TIMEZONE
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, Calendar.MAY);
calendar.set(Calendar.YEAR, 1921);
System.out.println(calendar.getTime());

Or am I am missing something?

mmakundi
Offline
Joined: 2007-08-14

Ah. Ok.

If you use this:

TimeZone.setDefault(TimeZone.getTimeZone("GMT+02:00"));

Then it works:

Sun May 01 00:00:00 GMT+02:00 1921
Sun May 01 00:00:00 GMT+02:00 1921

Also this works: TimeZone.setDefault(TimeZone.getTimeZone("EET"));

Sun May 01 00:00:00 EET 1921
Sun May 01 00:00:00 EET 1921

So what is required is using the TimeZone.setDefault ...

- Martin

mmakundi
Offline
Joined: 2007-08-14

Actually it is not a bug at all. It is indeed a feature, with purpose. If you use the local time somewhere you will experience this tweak, but ofcourse normal timezones are usable.

http://en.wikipedia.org/wiki/Zoneinfo

http://java.sun.com/developer/technicalArticles/Intl/USDST_Faq.html

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6281408

“In Java, the current standard time offset is used before 1900 to avoid some confusions with local mean time. But some time zones like Europe/Athens/Helsinki still used local mean time after 1900 and causes this problem.”

http://www.acad.bg/ftp/usr/share/lib/zoneinfo/europe

# Zone NAME GMTOFF RULES FORMAT [UNTIL]
Zone Europe/Helsinki 1:39:52 - LMT 1878 May 31
1:40 - HMT 1921 May # Helsinki Mean Time
2:00 Finland EET%s 1981 Mar 29 2:00
2:00 E-Eur EET%s