In my spring boot (2.1.4) project I have mysql database (5.7.21 - version of server) and following application properties:
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC
and
hibernate.jdbc.time-zone=UTC
.
Now when I save entitity records with @CreationTimestamp
using java (either by using @PostConstruct
or some controller/service logic) the dates are correctly converted to UTC
timezone and saved do database, and also retrieved in UTC
format (as I want them to be). However, when I add (other) records through data.sql
file. The corresponding timestamp fields have also UTC timezone, but this time the time is not properly converted to UTC (it's just my current time with timezone set to UTC).
How to properly set it for both situations to always convert local time to UTC, save it and always retrieve it as UTC from database?
Should changing mysql global timezone fix the issue?
Example:
I am in timezone: GMT+2 The records saved at 11:11:53 have following time set for:
1.) Java created records: 09:11:53
2.) data.sql created records: 11:11:53
Both are in UTC timezone (I can see it, when fetch in client), but only first is correct.