• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Siegfried Heintze
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going nuts trying to understand some problems with dates and time zones.

Is this the current documentation? http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html

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 hh:mm:ss.xxx 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?

Thanks,
siegfried

 
Narendira Sarma
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate/spring may return java.sql.Date (subclass of java.util.Date) instead of plain java.util.Date.
 
Siegfried Heintze
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



Thanks!
Siegfried
 
Paul Clapham
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably it's because Hibernate is actually returning a subclass of java.util.Date whose toString() method works differently.
 
Siegfried Heintze
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! You are right! Hibernate is using TimeStamp!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic