Skip to content

Commit 1fe4967

Browse files
authored
Merge pull request eugenp#5514 from eugenp/BAEL-2222-v2
update zonedatetime test
2 parents c3ead41 + 906082b commit 1fe4967

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
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"));
2023

2124
LocalDateTime localDateTime = LocalDateTime.now();
2225
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
2326

24-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss Z");
27+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss Z");
2528
String formattedString = zonedDateTime.format(formatter);
2629

27-
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss z");
30+
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss z");
2831
String formattedString2 = zonedDateTime.format(formatter2);
2932

3033
log.info(formattedString);
@@ -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)