import java.sql.Timestamp;
import java.time.OffsetDateTime;
-import java.time.ZoneOffset;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
{
if (odt == null)
return null;
- return Timestamp.from(odt.withOffsetSameInstant(ZoneOffset.UTC).toInstant());
+ return Timestamp.valueOf(odt.withOffsetSameInstant(TimeConversions.getZoneOffset()).toLocalDateTime());
}
@Override
{
if (ts == null)
return null;
- return OffsetDateTime.of(ts.toLocalDateTime(), ZoneOffset.UTC);
+ return OffsetDateTime.of(ts.toLocalDateTime(), TimeConversions.getZoneOffset());
}
}
--- /dev/null
+package de.juplo.jpa.converters;
+
+import java.time.Instant;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+
+
+/**
+ *
+ * @author Kai Moritz
+ */
+public class TimeConversions
+{
+ private static ZoneIdStrategy zoneStrategy =
+ new SystemDefaultZoneIdStrategy();
+ private static ZoneOffsetStrategy offsetStrategy =
+ new SystemDefaultZoneOffsetStrategy();
+
+
+ public static ZoneId getZoneId()
+ {
+ return zoneStrategy.getZoneId();
+ }
+
+ public static ZoneOffset getZoneOffset()
+ {
+ return offsetStrategy.getZoneOffset();
+ }
+
+
+ public static void setZoneIdStrategy(ZoneIdStrategy strategy)
+ {
+ if (strategy == null)
+ throw new IllegalArgumentException("Strategy must not be null!");
+ zoneStrategy = strategy;
+ }
+
+ public static void setZoneOffsetStrategy(ZoneOffsetStrategy strategy)
+ {
+ if (strategy == null)
+ throw new IllegalArgumentException("Strategy must not be null!");
+ offsetStrategy = strategy;
+ }
+
+
+ public interface ZoneIdStrategy
+ {
+ public ZoneId getZoneId();
+ }
+
+ public interface ZoneOffsetStrategy
+ {
+ public ZoneOffset getZoneOffset();
+ }
+
+
+ public static class SystemDefaultZoneIdStrategy
+ implements
+ ZoneIdStrategy
+ {
+ @Override
+ public ZoneId getZoneId()
+ {
+ return ZoneId.systemDefault();
+ }
+ }
+
+ public static class SystemDefaultZoneOffsetStrategy
+ implements
+ ZoneOffsetStrategy
+ {
+ @Override
+ public ZoneOffset getZoneOffset()
+ {
+ return ZoneOffset.systemDefault().getRules().getOffset(Instant.now());
+ }
+ }
+
+ public static class FixedZoneIdStrategy
+ implements
+ ZoneIdStrategy
+ {
+ private final ZoneId zone;
+
+ public FixedZoneIdStrategy(ZoneId zone)
+ {
+ this.zone = zone;
+ }
+
+ @Override
+ public ZoneId getZoneId()
+ {
+ return zone;
+ }
+ }
+
+ public static class FixedZoneOffsetStrategy
+ implements
+ ZoneOffsetStrategy
+ {
+ private final ZoneOffset offset;
+
+ public FixedZoneOffsetStrategy(ZoneOffset offset)
+ {
+ this.offset = offset;
+ }
+
+ @Override
+ public ZoneOffset getZoneOffset()
+ {
+ return offset;
+ }
+ }
+}
package de.juplo.jpa.converters;
import java.sql.Timestamp;
-import java.time.ZoneId;
import java.time.ZonedDateTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
{
if (zdt == null)
return null;
- return Timestamp.from(zdt.withZoneSameInstant(ZoneId.systemDefault()).toInstant());
+ return Timestamp.from(zdt.withZoneSameInstant(TimeConversions.getZoneId()).toInstant());
}
@Override
{
if (ts == null)
return null;
- return ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault());
+ return ZonedDateTime.ofInstant(ts.toInstant(), TimeConversions.getZoneId());
}
}