From: Kai Moritz Date: Mon, 21 Dec 2015 22:58:06 +0000 (+0100) Subject: Implemented an OffsetDateTime-to-GregorianCalendar converter X-Git-Tag: jpa-converters-1.0.0~19 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=a4526e622738d2360f2cd79407cfa3f8d406fa10;p=jpa-converters Implemented an OffsetDateTime-to-GregorianCalendar converter --- diff --git a/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java b/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java new file mode 100644 index 0000000..d49da18 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java @@ -0,0 +1,35 @@ +package de.juplo.jpa.converters; + +import java.time.OffsetDateTime; +import java.time.ZonedDateTime; +import java.util.GregorianCalendar; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link OffsetDateTime} to a {@link GregorianCalendar}, by first + * converting it into a {@link ZonedDateTime} and then converting it as + * suggested in the official Java 8 Time tutorial. + *

+ * The {@link GregorianCalendar} can then be persisted as + * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the + * {@link java.persist.Tmporal}-annotation. + * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html + * @author Kai Moritz + */ +@Converter(autoApply = true) +public class OffsetDateTimeConverter implements AttributeConverter +{ + @Override + public GregorianCalendar convertToDatabaseColumn(OffsetDateTime odt) + { + return GregorianCalendar.from(ZonedDateTime.from(odt)); + } + + @Override + public OffsetDateTime convertToEntityAttribute(GregorianCalendar calendar) + { + return OffsetDateTime.from(calendar.toZonedDateTime()); + } +}