1 package de.spring.example.persistence.converters;
3 import java.sql.Timestamp;
4 import java.time.LocalDateTime;
5 import java.time.OffsetDateTime;
6 import java.time.ZoneId;
7 import java.time.ZonedDateTime;
9 import javax.persistence.AttributeConverter;
10 import javax.persistence.Converter;
12 @Converter(autoApply = true)
13 public class OffsetDateTimeAttributeConverter implements AttributeConverter<OffsetDateTime, Timestamp> {
16 public Timestamp convertToDatabaseColumn(OffsetDateTime offsetDateTime) {
17 Timestamp timestamp = null;
19 if (offsetDateTime != null) {
20 timestamp = Timestamp.valueOf(offsetDateTime.toLocalDateTime());
27 public OffsetDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
28 OffsetDateTime offsetDateTime = null;
30 if (sqlTimestamp != null) {
31 final LocalDateTime localDateTime = sqlTimestamp.toLocalDateTime();
32 final ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
33 offsetDateTime = zonedDateTime.toOffsetDateTime();
36 return offsetDateTime;