BAEL-2222: Format ZonedDateTime to String

This commit is contained in:
j-bennett 2018-10-09 23:21:58 -04:00
parent 48b2316187
commit cf628344b6
2 changed files with 45 additions and 2 deletions

View File

@ -80,7 +80,7 @@
<joda-time.version>2.10</joda-time.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
</project>

View File

@ -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));
}
}