Background
I have a MySQL 8 server with default-time-zone set to +08:00, with existing dates store using the DATETIME
type.
I want to use Spring Data JPA to retrieve the date into a ZonedDateTime, e.g. 2024-07-08T08:00:00+08:00 or 2024-07-08T00:00:00Z. And eventually send this date time to the frontend for clients to determine how to render based on their time zones.
App servers are running in the cloud. TZ env vars are currently misconfigured I believe. Some servers are unset, some set to “Asia/Kuala_Lumpur”.
I figured I have to set the serverTimeZone in JDBC connection string and TZ correctly. But I kept getting wrong output. For example 2024-07-08T08:00:00
viewed in DBeaver is returned as 2024-07-08T08:00:00Z
from Spring Boot.
The closest thing I came to is by setting both JDBC and TZ to “Asia/Kuala_Lumpur”, and convert using ZoneId.systemDefault()
. If possible, I want to achieve this without adding a converter to all my entities.
Question
How to automatically retrieve a ZonedDateTime/OffsetDateTime based on the connection time zone in Spring Boot?