Joda time DateTime incorrectly stores in database

10,358

Solution 1

Ok. I have spend 8 hours to solve the problem. If your are using usertype project to persist JodaTime, you have to set databaseZone property of PersistentDateTime class equals to current application server timezone (not database!).

But it's better to use official hibernate support. It solves the problem out of the box because it uses java.utl.Date to persist DateTime and java.util.Date correctly persisted by default

Solution 2

Just set JPA properties:

<property name="jadira.usertype.autoRegisterUserTypes"
          value="true"/>
<property name="jadira.usertype.databaseZone"
          value="jvm"/>
<property name="jadira.usertype.javaZone"
          value="jvm"/>

Solution 3

I had the same issue. Specifying app and db zones in config solved the issue.

            <prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
            <prop key="jadira.usertype.databaseZone">America/Los_Angeles</prop>
            <prop key="jadira.usertype.javaZone">America/Los_Angeles</prop>

Solution 4

Try starting JVM with -Duser.timezone=UTC to JAVA_OPTS that way the time is in one zone and you can then do your operations on that to convert it to where ever you are.

Share:
10,358
fedor.belov
Author by

fedor.belov

Updated on June 05, 2022

Comments

  • fedor.belov
    fedor.belov almost 2 years

    I'm storing JodaTime DateTime field to timestamptz column by using org.jadira.usertype:usertype.jodatime:1.9. App server has +4 time zone. DB server +9 time zone. new DateTime() results in ${currentTime+1hour}+9 where +9 is time zone (correct value is ${currentTime+5hours)+9).

    I haven't found any related topics. java.util.Date stores correctly.

    Domain object has the following mapping property:

    static mapping = {
        dateCreated sqlType:'timestamptz'
    }
    

    How can I store DateTime correctly?