Implemented a ZonedDateTime-to-GregorianCalendar converter
[jpa-converters] / src / main / java / de / juplo / jpa / converters / ZonedDateTimeConverter.java
1 package de.juplo.jpa.converters;
2
3 import java.time.ZonedDateTime;
4 import java.util.GregorianCalendar;
5 import javax.persistence.AttributeConverter;
6 import javax.persistence.Converter;
7
8
9 /**
10  * Converts a {@link ZonedDateTime} to a {@link GregorianCalendar}, as suggested
11  * in the official Java 8 Time tutorial.
12  * <p>
13  * The {@link GregorianCalendar} can then be persisted as
14  * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the
15  * {@link java.persist.Tmporal}-annotation.
16  * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
17  * @author Kai Moritz
18  */
19 @Converter(autoApply = true)
20 public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime, GregorianCalendar>
21 {
22   @Override
23   public GregorianCalendar convertToDatabaseColumn(ZonedDateTime zdt)
24   {
25     return GregorianCalendar.from(zdt);
26   }
27
28   @Override
29   public ZonedDateTime convertToEntityAttribute(GregorianCalendar calendar)
30   {
31     return calendar.toZonedDateTime();
32   }
33 }