From 33e7ce89ce7593664f14023f38e1e48bbc2c968d Mon Sep 17 00:00:00 2001 From: RanjeetKaur17 Date: Wed, 12 Sep 2018 20:09:27 +0400 Subject: [PATCH] Adding Examples for ZonedDateTime, OffsetDateTime and OffsetTime --- .../java/com/baeldung/manifest/MANIFEST.MF | 1 + .../zoneddatetime/OffsetDateTimeExample.java | 17 +++++++++++ .../zoneddatetime/OffsetTimeExample.java | 18 +++++++++++ .../zoneddatetime/ZoneDateTimeExample.java | 21 +++++++++++++ .../OffsetDateTimeExampleUnitTest.java | 25 ++++++++++++++++ .../OffsetTimeExampleUnitTest.java | 25 ++++++++++++++++ .../ZoneDateTimeExampleUnitTest.java | 30 +++++++++++++++++++ 7 files changed, 137 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF create mode 100644 core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java create mode 100644 core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java create mode 100644 core-java/src/main/java/com/baeldung/zoneddatetime/ZoneDateTimeExample.java create mode 100644 core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF b/core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF new file mode 100644 index 0000000000..a363171952 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF @@ -0,0 +1 @@ +Main-Class: com.baeldung.manifest.AppExample diff --git a/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java new file mode 100644 index 0000000000..a22de0db18 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetDateTimeExample.java @@ -0,0 +1,17 @@ +package com.baeldung.zoneddatetime; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; + +public class OffsetDateTimeExample { + + public OffsetDateTime getCurrentTimeByZoneOffset(String region) { + LocalDateTime now = LocalDateTime.now(); + ZoneId zone = ZoneId.of(region); + ZoneOffset zoneOffSet= zone.getRules().getOffset(now); + OffsetDateTime date = OffsetDateTime.now(zoneOffSet); + return date; + } +} diff --git a/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java new file mode 100644 index 0000000000..7d926c0562 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/zoneddatetime/OffsetTimeExample.java @@ -0,0 +1,18 @@ +package com.baeldung.zoneddatetime; + +import java.time.LocalDateTime; +import java.time.OffsetTime; +import java.time.ZoneId; +import java.time.ZoneOffset; + +public class OffsetTimeExample { + + public OffsetTime getCurrentTimeByZoneOffset(String region) { + LocalDateTime now = LocalDateTime.now(); + ZoneId zone = ZoneId.of(region); + ZoneOffset zoneOffSet = zone.getRules() + .getOffset(now); + OffsetTime time = OffsetTime.now(zoneOffSet); + return time; + } +} diff --git a/core-java/src/main/java/com/baeldung/zoneddatetime/ZoneDateTimeExample.java b/core-java/src/main/java/com/baeldung/zoneddatetime/ZoneDateTimeExample.java new file mode 100644 index 0000000000..b54b8c5225 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/zoneddatetime/ZoneDateTimeExample.java @@ -0,0 +1,21 @@ +package com.baeldung.zoneddatetime; + +import java.time.ZoneId; +import java.time.ZonedDateTime; + +public class ZoneDateTimeExample { + + public ZonedDateTime getCurrentTimeByZoneId(String region) { + ZoneId zone = ZoneId.of(region); + ZonedDateTime date = ZonedDateTime.now(zone); + return date; + } + + public ZonedDateTime convertZonedDateTime(ZonedDateTime sourceDate, String destZone) { + + ZoneId destZoneId = ZoneId.of(destZone); + ZonedDateTime destDate = sourceDate.withZoneSameInstant(destZoneId); + + return destDate; + } +} diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java new file mode 100644 index 0000000000..c60f6967f9 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetDateTimeExampleUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.zoneddatetime; + +import static org.junit.Assert.assertTrue; + +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; + +import org.junit.Test; + +public class OffsetDateTimeExampleUnitTest { + + OffsetDateTimeExample offsetDateTimeExample = new OffsetDateTimeExample(); + + @Test + public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() { + String zone = "Europe/Berlin"; + OffsetDateTime time = offsetDateTimeExample.getCurrentTimeByZoneOffset(zone); + + assertTrue(time.getOffset() + .equals(ZoneId.of(zone) + .getRules() + .getOffset(LocalDateTime.now()))); + } +} diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java new file mode 100644 index 0000000000..0e1206dc5b --- /dev/null +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/OffsetTimeExampleUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.zoneddatetime; + +import static org.junit.Assert.assertTrue; + +import java.time.LocalDateTime; +import java.time.OffsetTime; +import java.time.ZoneId; + +import org.junit.Test; + +public class OffsetTimeExampleUnitTest { + + OffsetTimeExample offsetTimeExample = new OffsetTimeExample(); + + @Test + public void givenZoneOffset_whenGetCurrentTime_thenResultHasZone() { + String zone = "Europe/Berlin"; + OffsetTime time = offsetTimeExample.getCurrentTimeByZoneOffset(zone); + + assertTrue(time.getOffset() + .equals(ZoneId.of(zone) + .getRules() + .getOffset(LocalDateTime.now()))); + } +} diff --git a/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java b/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java new file mode 100644 index 0000000000..7f4f9cd17c --- /dev/null +++ b/core-java/src/test/java/com/baeldung/zoneddatetime/ZoneDateTimeExampleUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.zoneddatetime; + +import static org.junit.Assert.assertTrue; + +import java.time.ZoneId; +import java.time.ZonedDateTime; + +import org.junit.Test; + +public class ZoneDateTimeExampleUnitTest { + + ZoneDateTimeExample zoneDateTimeExample = new ZoneDateTimeExample(); + + @Test + public void givenZone_whenGetCurrentTime_thenResultHasZone() { + String zone = "Europe/Berlin"; + ZonedDateTime time = zoneDateTimeExample.getCurrentTimeByZoneId(zone); + assertTrue(time.getZone() + .equals(ZoneId.of(zone))); + } + + @Test + public void givenZones_whenConvertDateByZone_thenGetConstantDiff() { + String sourceZone = "Europe/Berlin"; + String destZone = "Asia/Tokyo"; + ZonedDateTime sourceDate = zoneDateTimeExample.getCurrentTimeByZoneId(sourceZone); + ZonedDateTime destDate = zoneDateTimeExample.convertZonedDateTime(sourceDate, destZone); + assertTrue(sourceDate.toInstant().compareTo(destDate.toInstant()) == 0); + } +}