package de.juplo.jpa.converters;
+import java.sql.Timestamp;
import java.time.Instant;
-import java.util.Date;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
- * Converts an {@link Instant} to 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.TIMESTAMP} with the help of the
- * {@link java.persist.Tmporal}-annotation.
- * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
+ * Converts an {@link Instant} to a {@link Timestamp}.
* @author Kai Moritz
*/
@Converter(autoApply = true)
-public class InstantConverter implements AttributeConverter<Instant, Date>
+public class InstantConverter implements AttributeConverter<Instant, Timestamp>
{
@Override
- public Date convertToDatabaseColumn(Instant instant)
+ public Timestamp convertToDatabaseColumn(Instant instant)
{
- return Date.from(instant);
+ return Timestamp.from(instant);
}
@Override
- public Instant convertToEntityAttribute(Date date)
+ public Instant convertToEntityAttribute(Timestamp ts)
{
- return date.toInstant();
+ return ts.toInstant();
}
}
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();
}
}
package de.juplo.jpa.converters;
-import java.time.Instant;
+import java.sql.Timestamp;
import java.time.LocalDateTime;
-import java.time.ZoneOffset;
-import java.util.Date;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
- * Converts a {@link LocalDateTime} to a {@link Date}, by converting it into
- * an {@Instant} for the GMT-timezone 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.TIMESTAMP} with the help of the
- * {@link java.persist.Tmporal}-annotation.
- * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
+ * Converts a {@link LocalDateTime} to a {@link Timestamp}.
* @author Kai Moritz
*/
@Converter(autoApply = true)
-public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Date>
+public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp>
{
@Override
- public Date convertToDatabaseColumn(LocalDateTime ldt)
+ public Timestamp convertToDatabaseColumn(LocalDateTime ldt)
{
- return Date.from(ldt.toInstant(ZoneOffset.UTC));
+ return Timestamp.valueOf(ldt);
}
@Override
- public LocalDateTime convertToEntityAttribute(Date date)
+ public LocalDateTime convertToEntityAttribute(Timestamp ts)
{
- return LocalDateTime.from(date.toInstant());
+ return ts.toLocalDateTime();
}
}
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.
- * <p>
- * 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<LocalTime, Date>
+public class LocalTimeConverter implements AttributeConverter<LocalTime, Time>
{
- 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();
}
}
package de.juplo.jpa.converters;
+import java.sql.Timestamp;
import java.time.OffsetDateTime;
-import java.time.ZonedDateTime;
-import java.util.GregorianCalendar;
+import java.time.ZoneOffset;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
- * Converts a {@link OffsetDateTime} to a {@link GregorianCalendar}, by first
- * converting it into a {@link ZonedDateTime} and then converting it as
- * suggested in the official Java 8 Time tutorial.
- * <p>
- * The {@link GregorianCalendar} can then be persisted as
- * {@link java.persistene.TemporalType.TIMESTAMP} with the help of the
- * {@link java.persist.Tmporal}-annotation.
- * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
+ * Converts a {@link OffsetDateTime} to a {@link Timestamp}.
* @author Kai Moritz
*/
@Converter(autoApply = true)
-public class OffsetDateTimeConverter implements AttributeConverter<OffsetDateTime, GregorianCalendar>
+public class OffsetDateTimeConverter implements AttributeConverter<OffsetDateTime, Timestamp>
{
@Override
- public GregorianCalendar convertToDatabaseColumn(OffsetDateTime odt)
+ public Timestamp convertToDatabaseColumn(OffsetDateTime odt)
{
- return GregorianCalendar.from(ZonedDateTime.from(odt));
+ return Timestamp.from(odt.withOffsetSameInstant(ZoneOffset.UTC).toInstant());
}
@Override
- public OffsetDateTime convertToEntityAttribute(GregorianCalendar calendar)
+ public OffsetDateTime convertToEntityAttribute(Timestamp ts)
{
- return OffsetDateTime.from(calendar.toZonedDateTime());
+ return OffsetDateTime.of(ts.toLocalDateTime(), ZoneOffset.UTC);
}
}
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);
}
}
package de.juplo.jpa.converters;
+import java.sql.Timestamp;
+import java.time.ZoneId;
import java.time.ZonedDateTime;
-import java.util.GregorianCalendar;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
- * Converts a {@link ZonedDateTime} to 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.TIMESTAMP} with the help of the
- * {@link java.persist.Tmporal}-annotation.
- * @see https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html
+ * Converts a {@link ZonedDateTime} to a {@link Timestamp}.
* @author Kai Moritz
*/
@Converter(autoApply = true)
-public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime, GregorianCalendar>
+public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime, Timestamp>
{
@Override
- public GregorianCalendar convertToDatabaseColumn(ZonedDateTime zdt)
+ public Timestamp convertToDatabaseColumn(ZonedDateTime zdt)
{
- return GregorianCalendar.from(zdt);
+ return Timestamp.from(zdt.withZoneSameInstant(ZoneId.systemDefault()).toInstant());
}
@Override
- public ZonedDateTime convertToEntityAttribute(GregorianCalendar calendar)
+ public ZonedDateTime convertToEntityAttribute(Timestamp ts)
{
- return calendar.toZonedDateTime();
+ return ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault());
}
}