From 211df8196faf7f9d4d7e1615cabf726a524f5422 Mon Sep 17 00:00:00 2001 From: vunamtien Date: Thu, 25 Apr 2024 23:01:54 +0700 Subject: [PATCH] [BAEL-7767] Convert-between-ZonedDateTime-and-Date (#16490) --- .../DateAndZonedDateTimeConverter.java | 17 +++++++++++ ...DateAndZonedDateTimeConverterUnitTest.java | 28 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 core-java-modules/core-java-datetime-conversion-2/src/main/java/com/baeldung/convertdateandzoneddatetime/DateAndZonedDateTimeConverter.java create mode 100644 core-java-modules/core-java-datetime-conversion-2/src/test/java/com/baeldung/convertdateandzoneddatetime/DateAndZonedDateTimeConverterUnitTest.java diff --git a/core-java-modules/core-java-datetime-conversion-2/src/main/java/com/baeldung/convertdateandzoneddatetime/DateAndZonedDateTimeConverter.java b/core-java-modules/core-java-datetime-conversion-2/src/main/java/com/baeldung/convertdateandzoneddatetime/DateAndZonedDateTimeConverter.java new file mode 100644 index 0000000000..e33d200856 --- /dev/null +++ b/core-java-modules/core-java-datetime-conversion-2/src/main/java/com/baeldung/convertdateandzoneddatetime/DateAndZonedDateTimeConverter.java @@ -0,0 +1,17 @@ +package com.baeldung.convertdateandzoneddatetime; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Date; + +public class DateAndZonedDateTimeConverter { + + public static Date convertToDate(ZonedDateTime zonedDateTime) { + return Date.from(zonedDateTime.toInstant()); + } + + public static ZonedDateTime convertToZonedDateTime(Date date, ZoneId zone) { + return date.toInstant().atZone(zone); + } + +} diff --git a/core-java-modules/core-java-datetime-conversion-2/src/test/java/com/baeldung/convertdateandzoneddatetime/DateAndZonedDateTimeConverterUnitTest.java b/core-java-modules/core-java-datetime-conversion-2/src/test/java/com/baeldung/convertdateandzoneddatetime/DateAndZonedDateTimeConverterUnitTest.java new file mode 100644 index 0000000000..3c1a9c9426 --- /dev/null +++ b/core-java-modules/core-java-datetime-conversion-2/src/test/java/com/baeldung/convertdateandzoneddatetime/DateAndZonedDateTimeConverterUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.convertdateandzoneddatetime; + +import org.junit.jupiter.api.Test; + +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Date; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DateAndZonedDateTimeConverterUnitTest { + + @Test + public void givenZonedDateTime_whenConvertToDate_thenCorrect() { + ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("UTC")); + Date date = DateAndZonedDateTimeConverter.convertToDate(zdt); + assertEquals(Date.from(zdt.toInstant()), date); + } + + @Test + public void givenDate_whenConvertToZonedDateTime_thenCorrect() { + Date date = new Date(); + ZoneId zoneId = ZoneId.of("UTC"); + ZonedDateTime zdt = DateAndZonedDateTimeConverter.convertToZonedDateTime(date, zoneId); + assertEquals(date.toInstant().atZone(zoneId), zdt); + } + +}