Hibernate – Date stored with Local Time Zone – Hibernate JPA

datehibernatejpatimezone

I have seen a number of threads related to the issue I have, but I couldn't figure out a way to resolve mine,

I have an attribute in an Entity of type Date. It's definition goes likes this,

@Column(name="CREATION_DATETIME")
@Temporal(TemporalType.TIMESTAMP)
private Date creationDateTime

When I print the date value, it is in local time zone, which is acceptable because the toString() in Date uses the default time zone ; which will be the time zone where the program is running. when stored in database it is storing the local time zone value; but I want it to be stored the original value without any timezone conversion. Hibernate handles the persistence here. why is this happening and how could it be resolved.?

Showing an example here to make my issue clear the date 2011-11-30T19:02:00+0000 is stored as 30-NOV-11 01.02.00.000000000 PM. the local time zone is CST, so it does the -0600 and stores it.

thanks
Sanjay

Best Answer

If you are mapping Date object with Hibernate, it will be stored without time zone. In fact, Date represents an instant in time, time zone is irrelevant and not stored.

After reading the date back from the database you can convert it to any time zone you want using Calendar class, but the original date in the database is basicaly a UNIX time (number of seconds/milliseconds from 1970 epoch).

See also: