package de.juplo.jpa.converters;
-import java.time.Instant;
+import java.sql.Timestamp;
import java.time.LocalDateTime;
-import java.time.ZoneOffset;
-import java.util.Date;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
- * Converts a {@link LocalDateTime} to a {@link Date}, by converting it into
- * an {@Instant} for the GMT-timezone and then convering that {@link Instant}
- * into a {@link Date} as suggested in the official Java 8 Time tutorial.
- * <p>
- * 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
+ * Converts a {@link LocalDateTime} to a {@link Timestamp}.
* @author Kai Moritz
*/
@Converter(autoApply = true)
-public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Date>
+public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp>
{
@Override
- public Date convertToDatabaseColumn(LocalDateTime ldt)
+ public Timestamp convertToDatabaseColumn(LocalDateTime ldt)
{
- return Date.from(ldt.toInstant(ZoneOffset.UTC));
+ return Timestamp.valueOf(ldt);
}
@Override
- public LocalDateTime convertToEntityAttribute(Date date)
+ public LocalDateTime convertToEntityAttribute(Timestamp ts)
{
- return LocalDateTime.from(date.toInstant());
+ return ts.toLocalDateTime();
}
}