Skip to content

Commit 277898a

Browse files
committed
update zonedatetime test
1 parent d8d58d8 commit 277898a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

java-dates/src/test/java/com/baeldung/zoneddatetime/ZonedDateTimeUnitTest.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package com.baeldung.zoneddatetime;
22

3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
35
import java.time.LocalDateTime;
46
import java.time.ZoneId;
57
import java.time.ZonedDateTime;
68
import java.time.format.DateTimeFormatter;
9+
import java.time.format.DateTimeParseException;
710
import java.util.logging.Logger;
811

9-
import org.junit.Test;
12+
import org.junit.jupiter.api.Test;
1013

1114
public 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

Comments
 (0)