File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Bug in java.util.Date.toString():? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bug in java.util.Date.toString():?" Watch "Bug in java.util.Date.toString():?" New topic

Bug in java.util.Date.toString():?

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 397
I'm going nuts trying to understand some problems with dates and time zones.

Is this the current documentation?

According to this, I should always get a date in this format when I call toString:

dow mon dd hh:mm:ss zzz yyyy

Sometimes I get this format on Linux RHEL 3 (yeah I know that is REALLY OLD but I cannot update) using Java 1.6.

yyyy mon dd hh:mm:ss.xxxx

Saving the date to an H2 database via hiberante/spring and reading it back again seams to change the format that is produced by java.util.Date.toString().

If I populate a date d1 with java.util.Calendar.getTime() and I save d1 to a H2 database and read it back into a second date object d2 I make the following observations:

(1) d1.toString() produces the dow mon dd hh:mm:ss zzz yyyy format
(2) d2.toString() produces yyy mo dd and this is contrary to the documentation!
(2) assertTrue(d1.equals(d2)) fails.
(3) If I extract the year, month, day, hour, minute, second from cal1 and cal2 where cal1 and cal2 are populated from d1 and d2 via Calendar.setTime, the year, month day, hour, minute, seconds match!

This is nuts! Have I discovered a bug?


Narendira Sarma

Joined: Nov 14, 2008
Posts: 18
A google search on "h2 database date format" presented this link. Data Types in H2

The documentation there clearly says the format for Date type is "yyyy-MM-dd" and for Timestamp it is "yyyy-MM-dd hh:mm:ss[.nnnnnnnnn]".

I guess its not a bug but normal behavior.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15096

Your link goes to the documentation for Java 1.4.2, but I doubt anything has changed here in later Java versions.

But if you're relying on how java.util.Date() converts itself to a string in its toString() method to store a date in the database, then you're doing something wrong.

Why are you trying to store a Date object as a string in the database? Doesn't the database have a DATE, DATETIME or TIMESTAMP column type?

If you for some reason still want to store it as a string, use a java.text.SimpleDateFormat object to convert the Date object to and from a string; SimpleDateFormat allows to to exactly specify the format.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Hibernate/spring may return java.sql.Date (subclass of java.util.Date) instead of plain java.util.Date.
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 397
Sorry! I'm not expressing myself clearly!

Hibernate is accepting a java.util.Date and retrieving a java.util.Date not a string. When I call the toString before and after hibernate the format of the date as generated by toString changes.

Why is this?

I'm not storing a string in the database.

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Presumably it's because Hibernate is actually returning a subclass of java.util.Date whose toString() method works differently.
Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 397
Thanks! You are right! Hibernate is using TimeStamp!
I agree. Here's the link:
subject: Bug in java.util.Date.toString():?
It's not a secret anymore!