1 package de.juplo.jpa.converters;
3 import java.time.Instant;
4 import java.time.LocalDateTime;
5 import java.time.ZoneOffset;
7 import javax.persistence.AttributeConverter;
8 import javax.persistence.Converter;
12 * Converts a {@link LocalDateTime} to a {@link Date}, by converting it into
13 * an {@Instant} for the GMT-timezone and then convering that {@link Instant}
14 * into a {@link Date} as suggested in the official Java 8 Time tutorial.
16 * The {@link Date} can then be persisted as
17 * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the
18 * {@link java.persist.Tmporal}-annotation.
19 * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
22 @Converter(autoApply = true)
23 public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Date>
26 public Date convertToDatabaseColumn(LocalDateTime ldt)
28 return Date.from(ldt.toInstant(ZoneOffset.UTC));
32 public LocalDateTime convertToEntityAttribute(Date date)
34 return LocalDateTime.from(date.toInstant());