From: Kai Moritz Date: Mon, 21 Dec 2015 20:29:51 +0000 (+0100) Subject: Implemented a ZonedDateTime-to-GregorianCalendar converter X-Git-Tag: jpa-converters-1.0.0~28 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=08c3d015cdfd31f8bfaed6a1ca6096bcd0ae519e;p=jpa-converters Implemented a ZonedDateTime-to-GregorianCalendar converter --- diff --git a/src/main/java/de/juplo/jpa/converters/ZonedDateTimeConverter.java b/src/main/java/de/juplo/jpa/converters/ZonedDateTimeConverter.java new file mode 100644 index 0000000..de9b726 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/ZonedDateTimeConverter.java @@ -0,0 +1,33 @@ +package de.juplo.jpa.converters; + +import java.time.ZonedDateTime; +import java.util.GregorianCalendar; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts a {@link ZonedDateTime} to a {@link GregorianCalendar}, 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 ZonedDateTimeConverter implements AttributeConverter +{ + @Override + public GregorianCalendar convertToDatabaseColumn(ZonedDateTime zdt) + { + return GregorianCalendar.from(zdt); + } + + @Override + public ZonedDateTime convertToEntityAttribute(GregorianCalendar calendar) + { + return calendar.toZonedDateTime(); + } +}