From f191427bead5042c28083ecb677bc46a9a0ca352 Mon Sep 17 00:00:00 2001 From: Olu <56642018+olu-damilare@users.noreply.github.com> Date: Sun, 27 Nov 2022 13:46:22 +0100 Subject: [PATCH] Convert between LocalDateTime and ZonedDateTime - BAEL-5879 (#13057) * Creating a Deep vs Shallow Copy of an Object in Java * completed tests for LinkedHashSet * update * updated PR * swapped the order of arguments for assertEquals() * completed code for Convert between LocalDateTime and ZonedDateTime * fixed module name * update * update * added more assertions * update * added more example * update * jenkin build error fix * jenkin build error fix * jenkins build fix * fixed suggested comments * update * update --- .../LocalDateTimeToZonedDateTimeUnitTest.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/localdatetimetozoneddatetime/LocalDateTimeToZonedDateTimeUnitTest.java diff --git a/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/localdatetimetozoneddatetime/LocalDateTimeToZonedDateTimeUnitTest.java b/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/localdatetimetozoneddatetime/LocalDateTimeToZonedDateTimeUnitTest.java new file mode 100644 index 0000000000..10a2e75372 --- /dev/null +++ b/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/localdatetimetozoneddatetime/LocalDateTimeToZonedDateTimeUnitTest.java @@ -0,0 +1,89 @@ +import org.junit.jupiter.api.Test; + +import java.time.*; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + + +public class LocalDateTimeToZonedDateTimeUnitTest { + + @Test + void whenConvertLocalDateTimeToZonedDateTimeWithAtZoneMethod_shouldConvertAndMaintainDateTimeValues(){ + LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 0, 30, 22); + ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Canada/Atlantic")); + + assertEquals(localDateTime.getYear(), zonedDateTime.getYear()); + assertEquals(localDateTime.getMonth(), zonedDateTime.getMonth()); + assertEquals(localDateTime.getDayOfMonth(), zonedDateTime.getDayOfMonth()); + assertEquals(localDateTime.getHour(), zonedDateTime.getHour()); + assertEquals(localDateTime.getMinute(), zonedDateTime.getMinute()); + assertEquals(localDateTime.getSecond(), zonedDateTime.getSecond()); + } + + @Test + void whenConvertLocalDateTimeToZonedDateTimeWithAtZoneAndwithZoneSameInstantMethods_shouldConvert(){ + LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 0, 30, 22); + ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Africa/Lagos")).withZoneSameInstant(ZoneId.of("Canada/Atlantic")); + + assertEquals("2021-12-31T19:30:22-04:00[Canada/Atlantic]", zonedDateTime.toString()); + assertEquals("-04:00", zonedDateTime.getOffset().toString()); + } + + @Test + void whenConvertLocalDateTimeToZonedDateTimeWithOfMethod_shouldConvert(){ + LocalDateTime localDateTime = LocalDateTime.of(2022, 11, 5, 7, 30, 22); + ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("Africa/Accra")).withZoneSameInstant(ZoneId.of("Africa/Lagos")); + + assertEquals("2022-11-05T08:30:22+01:00[Africa/Lagos]", zonedDateTime.toString()); + assertEquals(localDateTime.getYear(), zonedDateTime.getYear()); + } + + @Test + void whenConvertLocalDateTimeToZonedDateTimeWithOfInstantMethod_shouldConvert(){ + LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 5, 17, 30, 22); + ZoneId zoneId = ZoneId.of("Africa/Lagos"); + ZoneOffset zoneOffset = zoneId.getRules().getOffset(localDateTime); + ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(localDateTime, zoneOffset, zoneId); + + assertEquals("2022-01-05T17:30:22+01:00[Africa/Lagos]", zonedDateTime.toString()); + } + + @Test + void whenConvertLocalDateTimeToZonedDateTimeWithOfLocalMethod_shouldConvert(){ + LocalDateTime localDateTime = LocalDateTime.of(2022, 8, 25, 8, 35, 22); + ZoneId zoneId = ZoneId.of("Africa/Lagos"); + ZoneOffset zoneOffset = zoneId.getRules().getOffset(localDateTime); + ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(localDateTime, zoneId, zoneOffset); + + assertEquals("2022-08-25T08:35:22+01:00[Africa/Lagos]", zonedDateTime.toString()); + } + + @Test + void whenConvertLocalDateTimeToZonedDateTimeWithOfStrictMethod_shouldConvert(){ + LocalDateTime localDateTime = LocalDateTime.of(2002, 12, 25, 6, 18, 2); + ZoneId zoneId = ZoneId.of("Asia/Tokyo"); + ZoneOffset zoneOffset = zoneId.getRules().getOffset(localDateTime); + ZonedDateTime zonedDateTime = ZonedDateTime.ofStrict(localDateTime, zoneOffset, zoneId); + + assertEquals("2002-12-25T06:18:02+09:00[Asia/Tokyo]", zonedDateTime.toString()); + } + + @Test + void whenConvertLocalDateTimeToZonedDateTimeWithInvalidZoneOffSet_shouldThrowDateTimeException(){ + LocalDateTime localDateTime = LocalDateTime.of(2022, 12, 25, 6, 18, 2);; + ZoneId zoneId = ZoneId.of("Asia/Tokyo"); + ZoneOffset zoneOffset = ZoneOffset.UTC; + assertThrows(DateTimeException.class, () -> ZonedDateTime.ofStrict(localDateTime, zoneOffset, zoneId)); + } + + + @Test + void whenConvertZonedDateTimeToLocalDateTimeWithToLocalDateTimeMethod_shouldConvert(){ + ZonedDateTime zonedDateTime = ZonedDateTime.of(2011, 2, 12, 6, 14, 1, 58086000, ZoneId.of("Asia/Tokyo")); + LocalDateTime localDateTime = zonedDateTime.toLocalDateTime(); + + assertEquals("2011-02-12T06:14:01.058086+09:00[Asia/Tokyo]", zonedDateTime.toString()); + } +} \ No newline at end of file