Skip to content

Commit cf62834

Browse files
author
j-bennett
committed
BAEL-2222: Format ZonedDateTime to String
1 parent 48b2316 commit cf62834

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

java-dates/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<joda-time.version>2.10</joda-time.version>
8181
<!-- testing -->
8282
<assertj.version>3.6.1</assertj.version>
83-
<maven.compiler.source>9</maven.compiler.source>
84-
<maven.compiler.target>9</maven.compiler.target>
83+
<maven.compiler.source>1.9</maven.compiler.source>
84+
<maven.compiler.target>1.9</maven.compiler.target>
8585
</properties>
8686
</project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.zoneddatetime;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.ZoneId;
5+
import java.time.ZonedDateTime;
6+
import java.time.format.DateTimeFormatter;
7+
import java.util.logging.Logger;
8+
9+
import org.junit.Test;
10+
11+
public class ZonedDateTimeUnitTest {
12+
13+
private static final Logger log = Logger.getLogger(ZonedDateTimeUnitTest.class.getName());
14+
15+
@Test
16+
public void testZonedDateTimeToString() {
17+
18+
ZonedDateTime zonedDateTimeNow = ZonedDateTime.now(ZoneId.of("UTC"));
19+
ZonedDateTime zonedDateTimeOf = ZonedDateTime.of(2018, 01, 01, 0, 0, 0, 0, ZoneId.of("UTC"));
20+
21+
LocalDateTime localDateTime = LocalDateTime.now();
22+
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, ZoneId.of("UTC"));
23+
24+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss Z");
25+
String formattedString = zonedDateTime.format(formatter);
26+
27+
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/dd/yyyy - hh:mm:ss z");
28+
String formattedString2 = zonedDateTime.format(formatter2);
29+
30+
log.info(formattedString);
31+
log.info(formattedString2);
32+
33+
}
34+
35+
@Test
36+
public void testZonedDateTimeFromString() {
37+
38+
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2011-12-03T10:15:30+01:00", DateTimeFormatter.ISO_ZONED_DATE_TIME);
39+
40+
log.info(zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)