1 package de.juplo.jpa.converters;
3 import java.time.OffsetDateTime;
4 import java.time.ZonedDateTime;
5 import java.util.GregorianCalendar;
6 import javax.persistence.AttributeConverter;
7 import javax.persistence.Converter;
11 * Converts a {@link OffsetDateTime} to a {@link GregorianCalendar}, by first
12 * converting it into a {@link ZonedDateTime} and then converting it as
13 * suggested in the official Java 8 Time tutorial.
15 * The {@link GregorianCalendar} can then be persisted as
16 * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the
17 * {@link java.persist.Tmporal}-annotation.
18 * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
21 @Converter(autoApply = true)
22 public class OffsetDateTimeConverter implements AttributeConverter<OffsetDateTime, GregorianCalendar>
25 public GregorianCalendar convertToDatabaseColumn(OffsetDateTime odt)
27 return GregorianCalendar.from(ZonedDateTime.from(odt));
31 public OffsetDateTime convertToEntityAttribute(GregorianCalendar calendar)
33 return OffsetDateTime.from(calendar.toZonedDateTime());