From 277898ac24a3088b2adaaf6c6b71a1b59c5e64eb Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 21 Oct 2018 21:37:25 +0300 Subject: [PATCH] update zonedatetime test --- .../zoneddatetime/ZonedDateTimeUnitTest.java | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java b/java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java index 355fef35c6..c95d1a1203 100644 --- a/java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java +++ b/java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java @@ -1,19 +1,22 @@ package com.baeldung.zoneddatetime; +import static org.junit.jupiter.api.Assertions.assertThrows; + import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.logging.Logger; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class ZonedDateTimeUnitTest { private static final Logger log = Logger.getLogger(ZonedDateTimeUnitTest.class.getName()); @Test - public void testZonedDateTimeToString() { + public void givenZonedDateTime_whenConvertToString_thenOk() { ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC")); ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC")); @@ -33,9 +36,21 @@ public class ZonedDateTimeUnitTest { } @Test - public void testZonedDateTimeFromString() { + public void givenString_whenParseZonedDateTime_thenOk() { + ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00"); - ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00", DateTimeFormatter.ISO_ZONED_DATE_TIME); + log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); + } + + @Test + public void givenString_whenParseZonedDateTimeWithoutZone_thenException() { + assertThrows(DateTimeParseException.class, () -> ZonedDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME)); + } + + @Test + public void givenString_whenParseLocalDateTimeAtZone_thenOk() { + ZoneId timeZone = ZoneId.systemDefault(); + ZonedDateTime zonedDateTime = LocalDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_DATE_TIME).atZone(timeZone); log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME)); }