diff --git a/java-dates/pom.xml b/java-dates/pom.xml index 13e2a077e1..2618fad1d4 100644 --- a/java-dates/pom.xml +++ b/java-dates/pom.xml @@ -80,7 +80,7 @@ 2.10 3.6.1 - 9 - 9 + 1.9 + 1.9 diff --git a/java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java b/java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java new file mode 100644 index 0000000000..355fef35c6 --- /dev/null +++ b/java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.zoneddatetime; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.logging.Logger; + +import org.junit.Test; + +public class ZonedDateTimeUnitTest { + + private static final Logger log = Logger.getLogger(ZonedDateTimeUnitTest.class.getName()); + + @Test + public void testZonedDateTimeToString() { + + ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC")); + ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC")); + + LocalDateTime localDateTime = LocalDateTime.now(); + ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC")); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss Z"); + String formattedString = zonedDateTime.format(formatter); + + DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss z"); + String formattedString2 = zonedDateTime.format(formatter2); + + log.info(formattedString); + log.info(formattedString2); + + } + + @Test + public void testZonedDateTimeFromString() { + + 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)); + } + +}