package de.juplo.jpa.converters;
+import java.sql.Timestamp;
+import java.time.ZoneId;
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.
- * <p>
- * 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
+ * Converts a {@link ZonedDateTime} to a {@link Timestamp}.
* @author Kai Moritz
*/
@Converter(autoApply = true)
-public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime, GregorianCalendar>
+public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime, Timestamp>
{
@Override
- public GregorianCalendar convertToDatabaseColumn(ZonedDateTime zdt)
+ public Timestamp convertToDatabaseColumn(ZonedDateTime zdt)
{
- return GregorianCalendar.from(zdt);
+ return Timestamp.from(zdt.withZoneSameInstant(ZoneId.systemDefault()).toInstant());
}
@Override
- public ZonedDateTime convertToEntityAttribute(GregorianCalendar calendar)
+ public ZonedDateTime convertToEntityAttribute(Timestamp ts)
{
- return calendar.toZonedDateTime();
+ return ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault());
}
}