Merge pull request #5514 from eugenp/BAEL-2222-v2
update zonedatetime test
This commit is contained in:
commit
1fe49674b3
@ -1,19 +1,22 @@
|
|||||||
package com.baeldung.zoneddatetime;
|
package com.baeldung.zoneddatetime;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class ZonedDateTimeUnitTest {
|
public class ZonedDateTimeUnitTest {
|
||||||
|
|
||||||
private static final Logger log = Logger.getLogger(ZonedDateTimeUnitTest.class.getName());
|
private static final Logger log = Logger.getLogger(ZonedDateTimeUnitTest.class.getName());
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testZonedDateTimeToString() {
|
public void givenZonedDateTime_whenConvertToString_thenOk() {
|
||||||
|
|
||||||
ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC"));
|
ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC"));
|
||||||
ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC"));
|
ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC"));
|
||||||
@ -21,10 +24,10 @@ public class ZonedDateTimeUnitTest {
|
|||||||
LocalDateTime localDateTime = LocalDateTime.now();
|
LocalDateTime localDateTime = LocalDateTime.now();
|
||||||
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
|
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
|
||||||
|
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss Z");
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss Z");
|
||||||
String formattedString = zonedDateTime.format(formatter);
|
String formattedString = zonedDateTime.format(formatter);
|
||||||
|
|
||||||
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss z");
|
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - HH:mm:ss z");
|
||||||
String formattedString2 = zonedDateTime.format(formatter2);
|
String formattedString2 = zonedDateTime.format(formatter2);
|
||||||
|
|
||||||
log.info(formattedString);
|
log.info(formattedString);
|
||||||
@ -33,9 +36,21 @@ public class ZonedDateTimeUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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));
|
log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user