X-Git-Url: https://juplo.de/gitweb/?p=jpa-converters;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fjpa%2Fconverters%2FLocalTimeConverter.java;h=dc7ff9926fe8baaa97444e17287255f4ff1176a0;hp=60019f5b242aa28aeadae17c25db6664fa7e4272;hb=459423649abd980aee3f093606281819bbe4b83e;hpb=7a11c7d53d9546aa639a2e960811e321805ec643 diff --git a/src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java b/src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java index 60019f5..dc7ff99 100644 --- a/src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java +++ b/src/main/java/de/juplo/jpa/converters/LocalTimeConverter.java @@ -1,42 +1,27 @@ package de.juplo.jpa.converters; -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; +import java.sql.Time; import java.time.LocalTime; -import java.time.ZoneOffset; -import java.util.Date; import javax.persistence.AttributeConverter; import javax.persistence.Converter; /** - * Converts a {@link LocalTime} to a {@link Date}, by converting it into - * an {@Instant} for the GMT-timezone at the date of {@link Instant.EPOCH} and - * then convering that {@link Instant} into a {@link Date} as suggested in the - * official Java 8 Time tutorial. - *

- * The {@link Date} 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 a {@link LocalTime} to a {@link Time}. * @author Kai Moritz */ @Converter(autoApply = true) -public class LocalTimeConverter implements AttributeConverter +public class LocalTimeConverter implements AttributeConverter { - private final static LocalDate EPOCH = LocalDate.from(Instant.EPOCH); - - @Override - public Date convertToDatabaseColumn(LocalTime lt) + public Time convertToDatabaseColumn(LocalTime lt) { - return Date.from(LocalDateTime.of(EPOCH, lt).toInstant(ZoneOffset.UTC)); + return Time.valueOf(lt); } @Override - public LocalTime convertToEntityAttribute(Date date) + public LocalTime convertToEntityAttribute(Time time) { - return LocalTime.from(date.toInstant()); + return time.toLocalTime(); } }