When saving dates in UTC, H2 will not save in UTC. To force setting all datetimes in UTC timezone in Junit add below to test case
@BeforeClass public static void setTimezone() { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); }
To save all datetimes in UTC, add below
spring.jpa.properties.hibernate.jdbc.time_zone= UTC
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
OR use UTC specific SQL type in DB that will convert datetime from OS to UTC.JavaTimeModule
. Example: @Bean
@Primary
public ObjectMapper objectMapper() {
JavaTimeModule javaTimeModule = new JavaTimeModule();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper
.registerModule(javaTimeModule)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
return objectMapper;
}
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(
Instant.class,
new JsonSerializer<Instant>() {
@Override
public void serialize(
Instant instant,
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeString(instant.toString());
}
});
javaTimeModule.addDeserializer(
Instant.class,
new JsonDeserializer<Instant>() {
@Override
public Instant deserialize(
JsonParser jsonParser, DeserializationContext
deserializationContext)
throws IOException, JsonProcessingException {
return Instant.parse(jsonParser.getText());
}
});