package de.juplo.jpa.converters;
-import java.time.Instant;
+import java.sql.Date;
import java.time.LocalDate;
-import java.time.LocalDateTime;
-import java.time.LocalTime;
-import java.time.ZoneOffset;
-import java.util.Date;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
- * Converts a {@link LocalDate} to a {@link Date}, by converting it into
- * an {@Instant} for the GMT-timezone at {@link LocalTime.MIDNIGHT} 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.DATE} with the help of the
- * {@link java.persist.Tmporal}-annotation.
- * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
+ * Converts a {@link LocalDate} to a {@link Date}.
* @author Kai Moritz
*/
@Converter(autoApply = true)
@Override
public Date convertToDatabaseColumn(LocalDate ld)
{
- return Date.from(LocalDateTime.of(ld, LocalTime.MIDNIGHT).toInstant(ZoneOffset.UTC));
+ return Date.valueOf(ld);
}
@Override
public LocalDate convertToEntityAttribute(Date date)
{
- return LocalDate.from(date.toInstant());
+ return date.toLocalDate();
}
}