tyranitar tyranitar - 19 days ago 6
Java Question

Java Daylight saving and Hibernate

I found a way to create which date I want using the UTC timezone:
Java Date and Daylight Saving

So I can create "Sun Mar 25 02:00:00" but when I insert this date in Hibernate, it doesn't write it, seems that hibernate uses a different timezone that I can't overwrite.

My problem is that I can't insert in my Hibernate/H2 database this date: "Sun Mar 25 02:00:00" neither if I use the UTC timezone.

UPDATE 1:
I'm trying to insert these values:

Sun Mar 25 01:00:00 UTC 2012
Sun Mar 25 02:00:00 UTC 2012
Sun Mar 25 03:00:00 UTC 2012
Sun Mar 25 04:00:00 UTC 2012
Sun Mar 25 05:00:00 UTC 2012
Sun Mar 25 06:00:00 UTC 2012
Sun Mar 25 07:00:00 UTC 2012
Sun Mar 25 08:00:00 UTC 2012
Sun Mar 25 09:00:00 UTC 2012
Sun Mar 25 10:00:00 UTC 2012
Sun Mar 25 11:00:00 UTC 2012
Sun Mar 25 12:00:00 UTC 2012
Sun Mar 25 13:00:00 UTC 2012
Sun Mar 25 14:00:00 UTC 2012
Sun Mar 25 15:00:00 UTC 2012
Sun Mar 25 16:00:00 UTC 2012
Sun Mar 25 17:00:00 UTC 2012
Sun Mar 25 18:00:00 UTC 2012
Sun Mar 25 19:00:00 UTC 2012
Sun Mar 25 20:00:00 UTC 2012
Sun Mar 25 21:00:00 UTC 2012
Sun Mar 25 22:00:00 UTC 2012
Sun Mar 25 23:00:00 UTC 2012


but in the database there are these:

in the database

Answer

This is a "feature" of JDBC, not of Hibernate. Consequently you can change this behaviour in the jdbc options (jdbc connect string in hibernate.cfg.xml) and not in the Hibernate options. By default the date values are always saved in the time zone of the client application (and not in the time zone of the database, as it seems to be more logic). MySQL for example knows a jdbc connect option which forces the database to store the date values always in UTC. I don't know if H2 knows such an option, might be not.

In your example the 25th March was the first day with daylight time in that your. 25th March 02:00 didn't exist in your time zone, that's why you don't find it in the database.

If you don't want an option in the connect string (which has the disadvantage it might not be available on all databases) there are three possibilities how to go around the problem:

  1. Before inserting and after reading you do the time zone conversation in your program. For this additionally to your normal getter get/setMyTime() you write an extra getter and setter only for Hibernate get/setHibernateMyTime(), where you do the time zone conversion and which you use in the mapping files or in the annotations. For the time zone conversion you can use TimeZone.getOffset().

  2. Use Strings instead of dates. In this case you don't need to do the time zone conversion, but you have to convert strings into dates and vice versa.

  3. Run the application with UTC as local time. This has disadvantages if the local time of the user is needed anywhere, for example in the user interface.

We did solution 1. in our application.

Comments