# Having problems with timezone across hibernate

**URL:** <https://discourse.hibernate.org/t/having-problems-with-timezone-across-hibernate/7345>\
**Category:** Hibernate ORM\
**Created:** [March 6, 2023, 5:27pm UTC](https://discourse.hibernate.org/t/having-problems-with-timezone-across-hibernate/7345 "2023-03-06T17:27:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agreedSkiing](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/agreedskiing/32/2282_2.png) [@agreedSkiing](https://discourse.hibernate.org/u/agreedSkiing)\
**Post date:** [March 6, 2023, 5:27pm UTC](https://discourse.hibernate.org/t/having-problems-with-timezone-across-hibernate/7345/1 "2023-03-06T17:27:45Z")

</div>

Hi…  
So I have a question regarding my project [GitHub - agreedSkiing/hibernate-timezone-conversion-reproducer](https://github.com/agreedSkiing/hibernate-timezone-conversion-reproducer), I am experiencing a time-zone conversion bug/issue in Hibernate ORM 5, 6 and Reactive when my applications runs in the GMT+1 time-zone when the database connection does not have its connection time-zone specified (jdbc.timezone).

I know how to fix this issue and that is to set the database drivers time-zone to UTC since I’m using postgres but I think this issue is very odd since Hibernate ORM 6 removes one the time-zone conversion issues (🎉) that Hibernate ORM 5 has and Hibernate Reactive instead produces more failures (😖).

So is this an expected behaviour since I’m not setting the jdbc.timezone and every database driver has its own implementation for time conversion? Or is this an actual bug? Also yes I found the post from 2016 regarding jdbc timezone conversion ([How to store timestamps in UTC using the new hibernate.jdbc.time\_zone configuration property - In Relation To](https://in.relation.to/2016/09/12/jdbc-time-zone-configuration-property/)) which made me question the amount of time i put into this reproducer project.

Random endnotes: I do have some random notes in the readme file, but those were written before this post and I started thinking this was a Quarkus only issue but then found out that Spring Boot also had I just had to try with the basic hibernate implementation.

Also sorry in advance since this topic is also contains Hibernate Reactive.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [March 7, 2023, 8:52am UTC](https://discourse.hibernate.org/t/having-problems-with-timezone-across-hibernate/7345/2 "2023-03-07T08:52:34Z")

</div>

Time zone stuff is hard and requires a lot of thought which is time consuming. To make things easier, we usually recommend that the app and the DB should run in the UTC time zone. Note that you can configure that for the JVM by setting the system property `user.timezone=UTC` i.e. pass `-Duser.timezone=UTC` to the arguments of your app.

If you can’t do that for some reason, you can use the `hibernate.jdbc.time_zone` property instead. The effect of that is, that `Timestamp` values (which are in UTC), are converted to the specified time zone when sent to the database.  
Unless you use the `timestamptz` type in PostgreSQL, the database stores “local timestamps”. Local means that the stored timestamps are relative to the time zone configured on the DB. The `timestamptz` type on the other hand stores values in the UTC time zone.

As far as I understand your scenario, the problems you are facing can be solved by configuring the `hibernate.jdbc.time_zone` to match the time zone you configured on the DB, which is UTC.

---

<div class="post-metadata">

**Author:** ![agreedSkiing](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/agreedskiing/32/2282_2.png) [@agreedSkiing](https://discourse.hibernate.org/u/agreedSkiing)\
**Post date:** [March 7, 2023, 10:37am UTC](https://discourse.hibernate.org/t/having-problems-with-timezone-across-hibernate/7345/3 "2023-03-07T10:37:05Z")

</div>

Right my rambling got the better of me with the first post, this it’s not really a problem since I already know about the fix with the `hibernate.jdbc.time_zone` or setting the JVM to run with `user.timezone=UTC`, but since we are using `timestampz` in postgres for a new project I started to look into this issue more and started wondering if the problems we where facing when trying out different Hibernate implementations is a bug or not. What I gather from your post is that this is an expected behaviour and we should always ensure that either our JVM runs in UTC or the connection to the database has the correct time zone specified.

Currently since our local machines run in GMT+1 we are using the `hibernate.jdbc.time_zone` but when we deploy it to our production like environment everything runs in the UTC time environment and this was something we came to a conclusion to use before finding [How to store timestamps in UTC using the new hibernate.jdbc.time\_zone configuration property - In Relation To](https://in.relation.to/2016/09/12/jdbc-time-zone-configuration-property/).

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [March 8, 2023, 11:55am UTC](https://discourse.hibernate.org/t/having-problems-with-timezone-across-hibernate/7345/4 "2023-03-08T11:55:47Z")

</div>

Just for completeness, with Hibernate 6, you can use `java.time.Instant` in your model and it will use the `timestamptz` on PostgreSQL behind the scenes. There is no time zone configurations necessary when using just `Instant`

---

<div class="post-metadata">

**Author:** ![agreedSkiing](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/agreedskiing/32/2282_2.png) [@agreedSkiing](https://discourse.hibernate.org/u/agreedSkiing)\
**Post date:** [March 8, 2023, 1:14pm UTC](https://discourse.hibernate.org/t/having-problems-with-timezone-across-hibernate/7345/5 "2023-03-08T13:14:49Z")

</div>

That explains why one of the errors is gone in Hibernate 6, thanks!

---

<div class="post-metadata">

**Author:** ![agreedSkiing](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/agreedskiing/32/2282_2.png) [@agreedSkiing](https://discourse.hibernate.org/u/agreedSkiing)\
**Post date:** [March 8, 2023, 1:19pm UTC](https://discourse.hibernate.org/t/having-problems-with-timezone-across-hibernate/7345/6 "2023-03-08T13:19:35Z")

</div>

Either how as I understand time zones are difficult to handle and this is not a bug, and the solution to handle this the best is either setting the jdbc connections time zone with the help of `hibernate.jdbt.time_zone` or by setting it in the JVM with `-Duser.timezone=UTC` or in the java application with `TimeZone.setDefault(TimeZone.getTimeZone("UTC"))` or any other way that specifies the java applications time zone.

Thanks for being patient with my stupid question 🙂
