Wednesday, December 8, 2010

Mapping Dates and Time Zones with Hibernate, Part 2: Few Solutions

This blog post has moved to


  1. I believe there's a typo in your final solution:

    public Class returnedClass() {
    return ZuluCalendar.class;

    no mention of ZuluCalendar.class appears in anything previous.

  2. So I've added your class to my project as a custom user type and used the custom type on the attribute. I've set mySQL global and session timezones to GMT, but when I persist the object to the database the timezone is still being converted even when the calendar's timezone is set to GMT. If I don't set the timezone, it persists as I would expect.

    Calendar calGMT = Calendar.getInstance();
    calGMT.set(1951, 1, 15, 0,00,00);

    I would expect that when this is persisted that the value in the row in mysql would be:
    '1951-02-15 00:00:01'
    instead it is:
    '1951-02-14 18:00:01'

    If I don't set the calendar's timezone then I get the expected value of: '1951-02-15 00:00:01'

    My system timezone is CST.

    Know what is happening?

  3. It seems the gmt timezone is ignored when persisting. But from the code snippets provided, I cannot tell why. Pls provide more information: Did you use the user type as is? Please provide the code of the entity declaration and how you persist the entity.

  4. For me even saving a calendar object does not take the time zone into consideration. Doesn't matter what time zone the calendar object has, it always gets saved using the system/database time zone.

  5. This implementation appears to now be out of date with version 4.x of Hibernate (e.g. nullSafeSet impl must use SessionImplementor, etc).