Date stored in local time zone - Hibernate JPA

I saw several threads related to the problem that I have, but I could not figure out how to solve my problem,

I have an attribute in an Entity of type Date. I liked this definition,

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

When I print the date value, it is in the local time zone, which is acceptable because toString () in Date uses the default time zone; which will be the time zone in which the program runs. when saving to the database, it saves the value of the local time zone; but I want it to be kept the original value without any change in time zone. Here is Hibernate. why is this happening and how can this be solved?

Below is an example to make my problem clear. 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 has a value of -0600 and saves it.

thank you sanjay

+4
source share
1 answer

If you map a Date object to Hibernate, it will be stored without a time zone. In fact, Date represents a point in time; the time zone does not matter and is not saved.

After reading the date from the database, you can convert it to any time zone that you want to use with the Calendar class, but the original date in the database mainly refers to UNIX time (the number of seconds / milliseconds since the 1970s).

See also:

0
source

Source: https://habr.com/ru/post/1391882/


All Articles