I use Hibernate (4.2) as my persistence provider, and I have a JPA object that contains a Date field:
@Entity @Table(name = "MY_TABLE") public class MyTable implements Serializable { . . . @Temporal(TemporalType.TIMESTAMP) @Column(name = "START_DATE") private Date startDate; public Date getStartDate() { return startDate; } public void setStartDate(Date startDate) { this.startDate = startDate; } . . . }
The column corresponding to START_DATE is defined as START_DATE TIMESTAMP (without time zone).
I use Joda-Time (2.3) for my application to deal with the date (always in UTC), and just before saving the Entity I use the toDate() method of the Joda DateTime class to get the JDK Date object to obey the display:
public void myMethod(DateTime startDateUTC) { . . . MyTable table = table.setStartDate(startDateUTC.toDate()); . . . }
When I look at the stored value in the database, I notice that somewhere (JDK? Hibernate?) Converts the Date value using the default time zone of the JVM where the code is executed. In my case, this is America / Chicago.
The problem does occur near daylight saving time (DST). For example, if time is inside
2014-03-09T02:55:00Z
it is stored as
09-Mar-14 03:55:00
I would like it to be stored as
09-Mar-14 02:55:00
However, in the CDT, 2:55 a.m. March 9 does not exist ("Spring Forward"). So something (JDK? Hibernate?) Takes the date forward.
I would like the moment that is stored in the database to be in UTC. In the end, this is how I deal with it inside my application, but as soon as I submit it for storage, it will be converted to my default time zone.
Note: I cannot set TimeZone by default using
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
because the JVM I'm running on is shared between multiple applications.
How to store date in UTC without setting default JVM timezone in UTC?
java date datetime hibernate jpa
J steven perry
source share