Take the 2-minute tour ×

I'm using java, mysql, hibernate (3.6.x). On the java side I'm using java.sql.Timestamp objects. On the mysql side I'm using datetime columns.

I want hibernate to save/load these Timestamp objects using UTC time zone regardless of system/java/mysql time zone.

I found " JPA/Hibernate store date in UTC time zone " which was informative but lacking some final implementation info which I'm struggling to find.

I want to implement a UtcTimestampTypeDescriptor as shown in that thread and configure hibernate to use this instead of the normal TimestampTypeDescriptor.

How can I configure hibernate to use the UtcTimestamp type instead of the default Timestamp type?

share|improve this question
Why don't you try implementing community.jboss.org/wiki/UserTypeForNon-defaultTimeZone and then come up with the specific questions. –  ManuPK Mar 22 '12 at 14:04
I don't want to implement a simple usertype and annotate every date/tiemstamp property. Instead I want to implement a Descriptor and register it in hibernate instead of the default timstamp descriptor. The specific question is where do I register this type with hibernate so it replaces the default one? –  samz Mar 22 '12 at 16:42
I tried implementing the above link and it traces back to circa 2007 under early versions of Hibernate 3, the APIs of which have completely changed under JPA 2 and Hibnerate 4.x. There are numerous problems with the implementation that make it wholly incompatible. –  Darrell Teague Feb 23 '13 at 19:08
add comment

3 Answers

up vote 1 down vote accepted

I just added an answer on the question you reference that pretty clearly describes how to implement the solution.


If anything's not clear please let me know.

share|improve this answer
add comment

get class public class UtcTimestampType extends TimestampType from your link
and make this code

@org.hibernate.annotations.Type(type = "yourPackage.UtcTimestampType")   
public java.util.Date date;    

using annotations


<property name="date" column="yourColumn" type="yourPackage.UtcTimestampType" />  

using *.hbm.xml

share|improve this answer
I don't want to annotate every date/timestamp property I have. I want hibernate to always use the UTCTimestampType instead of the default type. –  samz Mar 22 '12 at 16:41
It is impossible. UTCTimestampType is custom user type. If you want, you can unpack Hibernate's library and hack it. –  Ilya Mar 22 '12 at 16:55
Look under that example, at the hibernate 3.6 example. There's an example of a descriptor, not a type. Then it says that when the app starts you can register this type instead of the default one but it doesn't say how/where.. That's basically my question. –  samz Mar 22 '12 at 16:59
You can do this, using class Configuration cfg = new Configuration().configure("your.cfg.xml"); cfg.registerTypeOverride(new UtcTimestampType()); –  Ilya Mar 22 '12 at 18:17
add comment

An alternative to implementing custom Hibernate types is to add the following JDBC options to your JDBC connection URL:


This will force your JDBC connection into the UTC timezone and ask MySQL to perform conversions from the JVM timezone. The net effect is that you can keep a local timezone on your JVM (e.g. for printing out log messages and so forth), while DATETIME columns will be persisted as UTC.

For example:

<bean id="hibernateAnalysisSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="hibernateProperties">
            <!-- Connection parameters -->
            <prop key="hibernate.connection.url">jdbc:mysql://hostname/databaseName?useTimezone=true&amp;serverTimezone=UTC</prop>
share|improve this answer
add comment

Your Answer


