Converters must convert to a data-type, know by the database
[jpa-converters] / src / main / java / de / juplo / jpa / converters / OffsetTimeConverter.java
index 4543950..92de0a9 100644 (file)
@@ -1,43 +1,29 @@
 package de.juplo.jpa.converters;
 
-import java.time.Instant;
-import java.time.LocalDate;
+import java.sql.Time;
 import java.time.OffsetTime;
 import java.time.ZoneOffset;
-import java.time.ZonedDateTime;
-import java.util.GregorianCalendar;
 import javax.persistence.AttributeConverter;
 import javax.persistence.Converter;
 
 
 /**
- * Converts an {@link OffsetTime} to a {@link GregorianCalendar}, by converting
- * it into a {@ZonedDateTime} at the date of {@link Instant.EPOCH} and
- * then convering that {@link ZonedDateTime} into 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.TIME} with the help of the
- * {@link java.persist.Tmporal}-annotation.
- * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
+ * Converts an {@link OffsetTime} to a {@link Time}.
  * @author Kai Moritz
  */
 @Converter(autoApply = true)
-public class OffsetTimeConverter implements AttributeConverter<OffsetTime, GregorianCalendar>
+public class OffsetTimeConverter implements AttributeConverter<OffsetTime, Time>
 {
-  private final static LocalDate EPOCH = LocalDate.from(Instant.EPOCH);
-
-
   @Override
-  public GregorianCalendar convertToDatabaseColumn(OffsetTime ot)
+  public Time convertToDatabaseColumn(OffsetTime ot)
   {
     ZoneOffset offset = ot.getOffset();
-    return GregorianCalendar.from(ZonedDateTime.of(EPOCH, ot.toLocalTime(), offset));
+    return Time.valueOf(ot.withOffsetSameInstant(ZoneOffset.UTC).toLocalTime());
   }
 
   @Override
-  public OffsetTime convertToEntityAttribute(GregorianCalendar calendar)
+  public OffsetTime convertToEntityAttribute(Time time)
   {
-    return OffsetTime.from(calendar.toInstant());
+    return OffsetTime.of(time.toLocalTime(), ZoneOffset.UTC);
   }
 }