Justinas Jakavonis
Justinas Jakavonis

Reputation: 8798

Java 8, Hibernate and PostgreSQL - how to store date with timezone?

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

Answers (1)

Kushan
Kushan

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

Related Questions