From 4151cacc0ef5231f5fbb80a24cb3c2a848d3c12c Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 21 Dec 2015 20:55:08 +0100 Subject: [PATCH] Implemented and Instant-to-Date converter --- .../jpa/converters/InstantConverter.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/de/juplo/jpa/converters/InstantConverter.java diff --git a/src/main/java/de/juplo/jpa/converters/InstantConverter.java b/src/main/java/de/juplo/jpa/converters/InstantConverter.java new file mode 100644 index 0000000..fe6bbd6 --- /dev/null +++ b/src/main/java/de/juplo/jpa/converters/InstantConverter.java @@ -0,0 +1,33 @@ +package de.juplo.jpa.converters; + +import java.time.Instant; +import java.util.Date; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + + +/** + * Converts an {@link Instant} to a {@link Date}, as suggested in + * the official Java 8 Time tutorial. + *

+ * The {@link Date} 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 InstantConverter implements AttributeConverter +{ + @Override + public Date convertToDatabaseColumn(Instant instant) + { + return Date.from(instant); + } + + @Override + public Instant convertToEntityAttribute(Date date) + { + return date.toInstant(); + } +} -- 2.20.1