database-rider icon indicating copy to clipboard operation
database-rider copied to clipboard

Dataset with UTC datetimes and spring.jpa.properties.hibernate.jdbc.time_zone=UTC result in wrong times

Open bergerst opened this issue 4 years ago • 0 comments

I have a dataset.yml similar to this:

test_data:
  - id: 1
    created_at: 2021-03-01T00:01:00.00+00:00

The Hibernate class for that would be something like

@Entity
@Table(name = "TEST_DATA")
public class TestData {
  @Id @GeneratedValue(strategy=GenerationType.AUTO)
  private int id;
  @Column
  private LocalDateTime createdAt = LocalDateTime.now();
}

In my Spring application.yaml, I have this setting:

spring:
  jpa:
    properties:
      hibernate:
        jdbc:
          time_zone: UTC

in order to store any Hibernate java.util.LocalDateTimes as UTC in the database.

My system's timezone is Europe/Berlin.

If I now query for the dataset entry using Hibernate, createdAt will have a LocalDateTime value of 2021-03-01T02:01:00.00 instead of the expected 2021-03-01T01:01:00.00.

If I remove the setting in the application.yaml, the time will be returned correctly, but the database entries will not contain UTC times. I currently have to set the timezone in the dataset to +01:00, even though I want to emulate 1 minute past midnight UTC time.

bergerst avatar May 10 '21 18:05 bergerst