EN
Java 8 - How to Format ZonedDateTime - examples
10
points
1. Overview
In this post we cover usage of ZonedDateTime class introduced in Java 8 with code examples.
2. ZonedDateTime with DateTimeFormatter
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Example1 {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
String pattern = "yyyy-MM-dd HH:mm:ss Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
String format = zonedDateTime.format(formatter);
System.out.println("ZonedDateTime.now: ");
System.out.println(zonedDateTime);
System.out.println("Formatted: ");
System.out.println(format);
}
}
Output:
ZonedDateTime.now:
2019-10-13T14:26:40.869+02:00[Europe/Belgrade]
Formatted:
2019-10-13 14:26:40 +0200
3. ZonedDateTime with DateTimeFormatter - zone - XXX eg +hh:mm
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Example2 {
public static void main(String[] args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
String pattern = "yyyy-MM-dd HH:mm:ss.SSS XXX";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
String format = zonedDateTime.format(formatter);
System.out.println("ZonedDateTime.now: ");
System.out.println(zonedDateTime);
System.out.println("Formatted: ");
System.out.println(format);
}
}
Output:
ZonedDateTime.now:
2019-10-13T14:27:00.347+02:00[Europe/Belgrade]
Formatted:
2019-10-13 14:27:00.347 +02:00
4. String to ZonedDateTime
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Example3 {
public static void main(String[] args) {
String dateTime = "2019-10-13 14:16:33 +0200";
String pattern = "yyyy-MM-dd HH:mm:ss Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, formatter);
System.out.println("ZonedDateTime: ");
System.out.println(zonedDateTime);
}
}
Output:
ZonedDateTime:
2019-10-13T14:16:33+02:00
5. String to ZonedDateTime with build-in formatter ISO_ZONED_DATE_TIME
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Example4 {
public static void main(String[] args) {
String dateTime = "2019-10-13T14:16:33.205+02:00[Europe/Belgrade]";
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, formatter);
System.out.println("ZonedDateTime: ");
System.out.println(zonedDateTime);
}
}
Output:
ZonedDateTime:
2019-10-13T14:16:33.205+02:00[Europe/Belgrade]
6. String to ZonedDateTime with build-in formatter ISO_OFFSET_DATE_TIME
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Example5 {
public static void main(String[] args) {
String dateTime = "2019-10-13T12:19:26+02:00";
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTime, formatter);
System.out.println("ZonedDateTime: ");
System.out.println(zonedDateTime);
}
}
Output:
ZonedDateTime:
2019-10-13T12:19:26+02:00