2

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.

thinktanker
  • 129
  • 2
  • 12

1 Answers1

1

Setting global timezone to UTC fixed the issue (all timestamps are now set to UTC, display in my local time in database, but get retrieved as UTC from database), what satisfies my conditions and is unified for both java and data.sql origin records.

To do it on windows I had to download timezones sql file from here: https://dev.mysql.com/downloads/timezones.html

,and run:

mysql -D mysql -uroot -pmypassword < timezone_posix.sql
SET @@global.time_zone = UTC;

Also this post helped me a lot:

How to set correct MySQL JDBC timezone in Spring Boot configuration

thinktanker
  • 129
  • 2
  • 12