From 2743bcc498997b56c4261855362e5ef6626e3d64 Mon Sep 17 00:00:00 2001 From: abdul0214 Date: Wed, 19 Jul 2023 22:57:13 +0200 Subject: [PATCH] BAEL-5928: Adds conversion b/w Epoch & LocalDateTime --- .../epochconversion/EpochToLocalDate.java | 24 ++++++++++++ .../epochconversion/LocalDateToEpoch.java | 18 +++++++++ .../EpochToLocalDateUnitTest.java | 39 +++++++++++++++++++ .../LocalDateTimeToEpochUnitTest.java | 37 ++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/epochconversion/EpochToLocalDate.java create mode 100644 core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/epochconversion/LocalDateToEpoch.java create mode 100644 core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/epochconversion/EpochToLocalDateUnitTest.java create mode 100644 core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/epochconversion/LocalDateTimeToEpochUnitTest.java diff --git a/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/epochconversion/EpochToLocalDate.java b/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/epochconversion/EpochToLocalDate.java new file mode 100644 index 0000000000..574693355d --- /dev/null +++ b/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/epochconversion/EpochToLocalDate.java @@ -0,0 +1,24 @@ +package com.baeldung.epochconversion; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; + +public class EpochToLocalDate { + + public static void main(String[] args) { + long millisSinceEpoch = 2131242L; + ZoneId zoneId = ZoneId.of("Europe/Amsterdam"); // or: ZoneId.systemDefault(); + // to get All Zone Ids available, we can use the function below + // Set allZoneIds = ZoneId.getAvailableZoneIds(); + LocalDate date = + Instant.ofEpochMilli(millisSinceEpoch) + .atZone(zoneId) + .toLocalDate(); + LocalDateTime time = + Instant.ofEpochMilli(millisSinceEpoch) + .atZone(zoneId) + .toLocalDateTime(); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/epochconversion/LocalDateToEpoch.java b/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/epochconversion/LocalDateToEpoch.java new file mode 100644 index 0000000000..b84cd5c1a7 --- /dev/null +++ b/core-java-modules/core-java-date-operations-3/src/main/java/com/baeldung/epochconversion/LocalDateToEpoch.java @@ -0,0 +1,18 @@ +package com.baeldung.epochconversion; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; + +public class LocalDateToEpoch { + + public static void main(String[] args) { + ZoneId zoneId = ZoneId.of("Europe/Amsterdam"); // or: ZoneId.systemDefault() + LocalDate date = LocalDate.now(); + long epochMilliSecondsAtDate = date.atStartOfDay(zoneId).toInstant().toEpochMilli(); + + // epoch for time + LocalDateTime time = LocalDateTime.parse("2019-11-15T13:15:30"); + long epochMilliSecondsAtTime = time.atZone(zoneId).toInstant().toEpochMilli(); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/epochconversion/EpochToLocalDateUnitTest.java b/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/epochconversion/EpochToLocalDateUnitTest.java new file mode 100644 index 0000000000..90ab056511 --- /dev/null +++ b/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/epochconversion/EpochToLocalDateUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.epochconversion; + +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class EpochToLocalDateUnitTest { + + @Test + void givenEpoch_thenComputeLocalDateCorrectly() { + ZoneId zoneId = ZoneId.systemDefault(); + LocalDate expectedDate = LocalDate.of(1995, 4, 15); + long date = expectedDate.atStartOfDay(zoneId).toInstant().toEpochMilli(); + + LocalDate actualDate = + Instant.ofEpochMilli(date) + .atZone(zoneId) + .toLocalDate(); + assertEquals(expectedDate, actualDate); + } + + @Test + void givenEpoch_thenComputeLocalDateTimeCorrectly() { + ZoneId zoneId = ZoneId.systemDefault(); + LocalDateTime expectedTime = LocalDateTime.parse("2019-11-15T13:15:30"); + long time = expectedTime.atZone(zoneId).toInstant().toEpochMilli(); + + LocalDateTime actualTime = + Instant.ofEpochMilli(time) + .atZone(zoneId) + .toLocalDateTime(); + assertEquals(expectedTime, actualTime); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/epochconversion/LocalDateTimeToEpochUnitTest.java b/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/epochconversion/LocalDateTimeToEpochUnitTest.java new file mode 100644 index 0000000000..8e9a4b01d0 --- /dev/null +++ b/core-java-modules/core-java-date-operations-3/src/test/java/com/baeldung/epochconversion/LocalDateTimeToEpochUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.epochconversion; + +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class LocalDateTimeToEpochUnitTest { + + @Test + void givenDate_thenComputeEpochCorrectly() { + ZoneId zoneId = ZoneId.of("Europe/Tallinn"); + long expectedEpoch = LocalDate.now().toEpochDay(); + LocalDateTime givenDate = Instant.ofEpochMilli(expectedEpoch) + .atZone(zoneId) + .toLocalDateTime(); + + long actualEpoch = givenDate.atZone(zoneId).toInstant().toEpochMilli(); + assertEquals(expectedEpoch, actualEpoch); + } + + @Test + void givenTime_thenComputeEpochCorrectly() { + ZoneId zoneId = ZoneId.of("Europe/Amsterdam"); + long expectedEpoch = Instant.now().toEpochMilli(); + LocalDateTime givenTime = Instant.ofEpochMilli(expectedEpoch) + .atZone(zoneId) + .toLocalDateTime(); + + long actualEpoch = givenTime.atZone(zoneId).toInstant().toEpochMilli(); + assertEquals(expectedEpoch, actualEpoch); + } +} \ No newline at end of file