Reputation: 8798
I use Java 8, Hibernate 5.1.0.Final and PostgreSQL. I am considering to store date time with timezone to the database.
I have tried
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created")
private Date created;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created")
private OffsetDateTime created;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created")
private ZonedDateTime created;
I use hibernate.hbm2ddl.auto update
for creating tables, columns, etc. All above mentioned options create column with timestamp without timezone
. How to create automatically timestamp with timezone
?
What is the best way to store time and timezone in a database?
Upvotes: 0
Views: 1331
Reputation: 10695
Normally hibernate store dates using UTC. Then you need to convert all time zones into UTC.
On startup,
TimeZone.setDefault(TimeZone.getTimeZone("ETC/UTC"));
Upvotes: 1