Video in progress
What time zone problems come up when persisting `LocalDateTime`, `Instant` and `OffsetDateTime` fields?
LocalDateTime has no zone: it's stored exactly as you write it, and its meaning shifts if the server's zone changes. Instant is an exact point in time, but the JVM zone can interfere when it's passed to JDBC. Setting hibernate.jdbc.time_zone=UTC and making the column timestamptz is the safest route.
📖 Answer
This happened on one project: the servers were on Tashkent time, then moved to Kubernetes and the containers started in UTC. All the new rows in the LocalDateTime columns went 5 hours "back", while the old rows stayed where they were. Reports got mixed up.
A quick rundown of the types:
LocalDateTimeis "the clock on the wall", with no zone. Good for data like a meeting time, bad for the time an event happened.Instantis an exact point in UTC. The right choice for audit andcreatedAt.OffsetDateTime/ZonedDateTimecarry an offset. Hibernate 6 controls how they are stored withhibernate.timezone.default_storage(for exampleNORMALIZE_UTCorNATIVE).
This is how I set it up in practice:
spring:
jpa:
properties:
hibernate.jdbc.time_zone: UTC
@Column(name = "created_at", nullable = false)
private Instant createdAt;
On PostgreSQL the column should be timestamptz. The name is misleading: it doesn't store the zone, it converts the value to UTC and stores that, which is exactly what you want for Instant.
Add the zone when showing the value to the user (in the UI or DTO layer), not in the entity. And in tests, try once with -Duser.timezone deliberately set to a different zone. This bug shows up very quickly.
Sign up to mark studied questions and save your progress.