Implemented a mechanism to set the offset/time-zone to use for conversions
authorKai Moritz <kai@juplo.de>
Tue, 15 Aug 2017 17:58:02 +0000 (19:58 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 15 Aug 2017 18:05:46 +0000 (20:05 +0200)
src/main/java/de/juplo/jpa/converters/OffsetDateTimeConverter.java
src/main/java/de/juplo/jpa/converters/TimeConversions.java [new file with mode: 0644]
src/main/java/de/juplo/jpa/converters/ZonedDateTimeConverter.java

index 3228aca..3c4365f 100644 (file)
@@ -2,7 +2,6 @@ package de.juplo.jpa.converters;
 
 import java.sql.Timestamp;
 import java.time.OffsetDateTime;
-import java.time.ZoneOffset;
 import javax.persistence.AttributeConverter;
 import javax.persistence.Converter;
 
@@ -19,7 +18,7 @@ public class OffsetDateTimeConverter implements AttributeConverter<OffsetDateTim
   {
     if (odt == null)
       return null;
-    return Timestamp.from(odt.withOffsetSameInstant(ZoneOffset.UTC).toInstant());
+    return Timestamp.valueOf(odt.withOffsetSameInstant(TimeConversions.getZoneOffset()).toLocalDateTime());
   }
 
   @Override
@@ -27,6 +26,6 @@ public class OffsetDateTimeConverter implements AttributeConverter<OffsetDateTim
   {
     if (ts == null)
       return null;
-    return OffsetDateTime.of(ts.toLocalDateTime(), ZoneOffset.UTC);
+    return OffsetDateTime.of(ts.toLocalDateTime(), TimeConversions.getZoneOffset());
   }
 }
diff --git a/src/main/java/de/juplo/jpa/converters/TimeConversions.java b/src/main/java/de/juplo/jpa/converters/TimeConversions.java
new file mode 100644 (file)
index 0000000..03dfd88
--- /dev/null
@@ -0,0 +1,114 @@
+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;
+    }
+  }
+}
index 3816d1b..69908b8 100644 (file)
@@ -1,7 +1,6 @@
 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;
@@ -19,7 +18,7 @@ public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime,
   {
     if (zdt == null)
       return null;
-    return Timestamp.from(zdt.withZoneSameInstant(ZoneId.systemDefault()).toInstant());
+    return Timestamp.from(zdt.withZoneSameInstant(TimeConversions.getZoneId()).toInstant());
   }
 
   @Override
@@ -27,6 +26,6 @@ public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime,
   {
     if (ts == null)
       return null;
-    return ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault());
+    return ZonedDateTime.ofInstant(ts.toInstant(), TimeConversions.getZoneId());
   }
 }