11package com .baeldung .zoneddatetime ;
22
3+ import static org .junit .jupiter .api .Assertions .assertThrows ;
4+
35import java .time .LocalDateTime ;
46import java .time .ZoneId ;
57import java .time .ZonedDateTime ;
68import java .time .format .DateTimeFormatter ;
9+ import java .time .format .DateTimeParseException ;
710import java .util .logging .Logger ;
811
9- import org .junit .Test ;
12+ import org .junit .jupiter . api . Test ;
1013
1114public class ZonedDateTimeUnitTest {
1215
1316 private static final Logger log = Logger .getLogger (ZonedDateTimeUnitTest .class .getName ());
1417
1518 @ Test
16- public void testZonedDateTimeToString () {
19+ public void givenZonedDateTime_whenConvertToString_thenOk () {
1720
1821 ZonedDateTime zonedDateTimeNow = ZonedDateTime .now (ZoneId .of ("UTC" ));
1922 ZonedDateTime zonedDateTimeOf = ZonedDateTime .of (2018 , 01 , 01 , 0 , 0 , 0 , 0 , ZoneId .of ("UTC" ));
@@ -33,9 +36,21 @@ public void testZonedDateTimeToString() {
3336 }
3437
3538 @ Test
36- public void testZonedDateTimeFromString () {
39+ public void givenString_whenParseZonedDateTime_thenOk () {
40+ ZonedDateTime zonedDateTime = ZonedDateTime .parse ("2011-12-03T10:15:30+01:00" );
3741
38- ZonedDateTime zonedDateTime = ZonedDateTime .parse ("2011-12-03T10:15:30+01:00" , DateTimeFormatter .ISO_ZONED_DATE_TIME );
42+ log .info (zonedDateTime .format (DateTimeFormatter .ISO_ZONED_DATE_TIME ));
43+ }
44+
45+ @ Test
46+ public void givenString_whenParseZonedDateTimeWithoutZone_thenException () {
47+ assertThrows (DateTimeParseException .class , () -> ZonedDateTime .parse ("2011-12-03T10:15:30" , DateTimeFormatter .ISO_DATE_TIME ));
48+ }
49+
50+ @ Test
51+ public void givenString_whenParseLocalDateTimeAtZone_thenOk () {
52+ ZoneId timeZone = ZoneId .systemDefault ();
53+ ZonedDateTime zonedDateTime = LocalDateTime .parse ("2011-12-03T10:15:30" , DateTimeFormatter .ISO_DATE_TIME ).atZone (timeZone );
3954
4055 log .info (zonedDateTime .format (DateTimeFormatter .ISO_ZONED_DATE_TIME ));
4156 }
0 commit comments