Java Convert ZonedDateTime to String
Tags: Java ZonedDateTime String Java 8 DateTimeFormatter
In this Java core tutorial we learn how to format a java.time.ZonedDateTime object into a date time String in Java programming language with different solutions and example Java code.
Table of contents
- Convert ZonedDateTime to String with Custom Date Time Pattern
- Convert ZonedDateTime to String in Localized Pattern
- Convert ZonedDateTime to String with Predefined DateTimeFormatter Constants
- Convert ZonedDateTime to String using toString() method
Convert ZonedDateTime to String with Custom Date Time Pattern
In Java with a given ZonedDateTime object we can use the ZonedDateTime.format(DateTimeFormatter formatter) method to format it to a String in custom date time pattern defined by java.time.format.DateTimeFormatter object. In the following Java program we show how to convert ZonedDateTime object to different custom date time pattern String.
ConvertZonedDateTimeToStringExample1.java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertZonedDateTimeToStringExample1 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss Z");
String zonedDateTimeString1 = zonedDateTime.format(dateTimeFormatter1);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("dd-M-yyyy hh:mm:ss a X");
String zonedDateTimeString2 = zonedDateTime.format(dateTimeFormatter2);
DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm:ss VV");
String zonedDateTimeString3 = zonedDateTime.format(dateTimeFormatter3);
DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("EEEE, MMM dd yyyy HH:mm:ss O");
String zonedDateTimeString4 = zonedDateTime.format(dateTimeFormatter4);
DateTimeFormatter dateTimeFormatter5 = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-SSSSSSS-z");
String zonedDateTimeString5 = zonedDateTime.format(dateTimeFormatter5);
System.out.println("dd/MM/yyyy HH:mm:ss Z -> " + zonedDateTimeString1);
System.out.println("dd-M-yyyy hh:mm:ss a X -> " + zonedDateTimeString2);
System.out.println("E, MMM dd yyyy HH:mm:ss VV -> " + zonedDateTimeString3);
System.out.println("EEEE, MMM dd yyyy HH:mm:ss O -> " + zonedDateTimeString4);
System.out.println("yyyy-MM-dd-HH-mm-ss-SSSSSSS-z -> " + zonedDateTimeString5);
}
}
dd/MM/yyyy HH:mm:ss Z -> 07/05/2022 14:38:25 +0700
dd-M-yyyy hh:mm:ss a X -> 07-5-2022 02:38:25 pm +07
E, MMM dd yyyy HH:mm:ss VV -> Sat, May 07 2022 14:38:25 Asia/Bangkok
EEEE, MMM dd yyyy HH:mm:ss O -> Saturday, May 07 2022 14:38:25 GMT+7
yyyy-MM-dd-HH-mm-ss-SSSSSSS-z -> 2022-05-07-14-38-25-9035725-ICT
Convert ZonedDateTime to String in Localized Pattern
With java.time.format.DateTimeFormatter class we can get a specified localized date time format patterns in different style using the DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateTimeStyle) method.
Below are the list of format styles we can use to format ZonedDateTime object
- FormatStyle.FULL to format full text style such as Monday, 25 April 2022 at 10:49:08 pm Central European Summer Time
- FormatStyle.LONG to format long text style such as 25 April 2022 at 10:49:08 pm CEST
- FormatStyle.MEDIUM to format medium text style such as 25 Apr 2022, 10:49:08 pm
- FormatStyle.SHORT to format short text style such as 25/4/22, 10:49 pm
In the following Java code we show how to use DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateTimeStyle) method with FormatStyle.FULL to format a ZonedDateTime object to String.
ConvertZonedDateTimeToStringExample2.java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class ConvertZonedDateTimeToStringExample2 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2022-04-25T22:49:08.176475400+02:00[Europe/Paris]");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
String zonedDateTimeString = zonedDateTime.format(dateTimeFormatter);
System.out.println(zonedDateTimeString);
}
}
Monday, 25 April 2022 at 10:49:08 pm Central European Summer Time
In the following Java code we show how to use DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateTimeStyle) method with FormatStyle.LONG to format a ZonedDateTime object to String.
ConvertZonedDateTimeToStringExample3.java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class ConvertZonedDateTimeToStringExample3 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2022-04-25T22:49:08.176475400+02:00[Europe/Paris]");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
String zonedDateTimeString = zonedDateTime.format(dateTimeFormatter);
System.out.println(zonedDateTimeString);
}
}
25 April 2022 at 10:49:08 pm CEST
In the following Java code we show how to use DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateTimeStyle) method with FormatStyle.MEDIUM to format a ZonedDateTime object to String.
ConvertZonedDateTimeToStringExample4.java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class ConvertZonedDateTimeToStringExample4 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2022-04-25T22:49:08.176475400+02:00[Europe/Paris]");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
String zonedDateTimeString = zonedDateTime.format(dateTimeFormatter);
System.out.println(zonedDateTimeString);
}
}
25 Apr 2022, 10:49:08 pm
In the following Java code we show how to use DateTimeFormatter.ofLocalizedDateTime(FormatStyle dateTimeStyle) method with FormatStyle.SHORT to format a ZonedDateTime object to String.
ConvertZonedDateTimeToStringExample5.java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class ConvertZonedDateTimeToStringExample5 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2022-04-25T22:49:08.176475400+02:00[Europe/Paris]");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
String zonedDateTimeString = zonedDateTime.format(dateTimeFormatter);
System.out.println(zonedDateTimeString);
}
}
25/4/22, 10:49 pm
Convert ZonedDateTime to String with Predefined DateTimeFormatter Constants
The java.time.format.DateTimeFormatter class provides predefined constants which we can use to format a ZonedDateTime object.
- DateTimeFormatter.ISO_LOCAL_DATE to format a ZonedDateTime object to String in ISO-8601 extended local date format.
- DateTimeFormatter.ISO_OFFSET_DATE to format a ZonedDateTime object to String in ISO-8601 extended offset date format.
- DateTimeFormatter.ISO_DATE to format a ZonedDateTime object to String in ISO-8601 extended date format.
- DateTimeFormatter.ISO_LOCAL_TIME to format a ZonedDateTime object to String in ISO-8601 extended local time format.
- DateTimeFormatter.ISO_OFFSET_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset time format.
- DateTimeFormatter.ISO_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset time format.
- DateTimeFormatter.ISO_LOCAL_DATE_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset date-time format.
- DateTimeFormatter.ISO_OFFSET_DATE_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset date-time format.
- DateTimeFormatter.ISO_ZONED_DATE_TIME to format a ZonedDateTime object to String in ISO-8601 extended offset date-time format to add the time-zone.
- DateTimeFormatter.ISO_DATE_TIME to format a ZonedDateTime object to String in ISO-8601 extended local or offset date-time format, as well as the extended non-ISO form specifying the time-zone.
- DateTimeFormatter.ISO_ORDINAL_DATE to format a ZonedDateTime object to String in ISO-8601 extended ordinal date format.
- DateTimeFormatter.ISO_WEEK_DATE to format a ZonedDateTime object to String in ISO-8601 extended week-based date format.
- DateTimeFormatter.ISO_INSTANT to format a ZonedDateTime object to String in ISO-8601 instant format.
- DateTimeFormatter.BASIC_ISO_DATE to format a ZonedDateTime object to String in ISO-8601 basic local date format.
- DateTimeFormatter.RFC_1123_DATE_TIME to format a ZonedDateTime object to String in RFC-1123 format.
In the following Java program we show how to use the DateTimeFormatter constants above.
ConvertZonedDateTimeToStringExample6.java
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ConvertZonedDateTimeToStringExample6 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2022-04-25T22:49:08.176475400+02:00[Europe/Paris]");
String zonedDateTimeString1 = zonedDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
String zonedDateTimeString2 = zonedDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE);
String zonedDateTimeString3 = zonedDateTime.format(DateTimeFormatter.ISO_DATE);
String zonedDateTimeString4 = zonedDateTime.format(DateTimeFormatter.ISO_LOCAL_TIME);
String zonedDateTimeString5 = zonedDateTime.format(DateTimeFormatter.ISO_OFFSET_TIME);
String zonedDateTimeString6 = zonedDateTime.format(DateTimeFormatter.ISO_TIME);
String zonedDateTimeString7 = zonedDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
String zonedDateTimeString8 = zonedDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
String zonedDateTimeString9 = zonedDateTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
String zonedDateTimeString10 = zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
String zonedDateTimeString11 = zonedDateTime.format(DateTimeFormatter.ISO_ORDINAL_DATE);
String zonedDateTimeString12 = zonedDateTime.format(DateTimeFormatter.ISO_WEEK_DATE);
String zonedDateTimeString13 = zonedDateTime.format(DateTimeFormatter.ISO_INSTANT);
String zonedDateTimeString14 = zonedDateTime.format(DateTimeFormatter.BASIC_ISO_DATE);
String zonedDateTimeString15 = zonedDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME);
System.out.println("DateTimeFormatter.ISO_LOCAL_DATE -> " + zonedDateTimeString1);
System.out.println("DateTimeFormatter.ISO_OFFSET_DATE -> " + zonedDateTimeString2);
System.out.println("DateTimeFormatter.ISO_DATE -> " + zonedDateTimeString3);
System.out.println("DateTimeFormatter.ISO_LOCAL_TIME -> " + zonedDateTimeString4);
System.out.println("DateTimeFormatter.ISO_OFFSET_TIME -> " + zonedDateTimeString5);
System.out.println("DateTimeFormatter.ISO_TIME -> " + zonedDateTimeString6);
System.out.println("DateTimeFormatter.ISO_LOCAL_DATE_TIME -> " + zonedDateTimeString7);
System.out.println("DateTimeFormatter.ISO_OFFSET_DATE_TIME -> " + zonedDateTimeString8);
System.out.println("DateTimeFormatter.ISO_ZONED_DATE_TIME -> " + zonedDateTimeString9);
System.out.println("DateTimeFormatter.ISO_DATE_TIME -> " + zonedDateTimeString10);
System.out.println("DateTimeFormatter.ISO_ORDINAL_DATE -> " + zonedDateTimeString11);
System.out.println("DateTimeFormatter.ISO_WEEK_DATE -> " + zonedDateTimeString12);
System.out.println("DateTimeFormatter.ISO_INSTANT -> " + zonedDateTimeString13);
System.out.println("DateTimeFormatter.BASIC_ISO_DATE -> " + zonedDateTimeString14);
System.out.println("DateTimeFormatter.RFC_1123_DATE_TIME -> " + zonedDateTimeString15);
}
}
DateTimeFormatter.ISO_LOCAL_DATE -> 2022-04-25
DateTimeFormatter.ISO_OFFSET_DATE -> 2022-04-25+02:00
DateTimeFormatter.ISO_DATE -> 2022-04-25+02:00
DateTimeFormatter.ISO_LOCAL_TIME -> 22:49:08.1764754
DateTimeFormatter.ISO_OFFSET_TIME -> 22:49:08.1764754+02:00
DateTimeFormatter.ISO_TIME -> 22:49:08.1764754+02:00
DateTimeFormatter.ISO_LOCAL_DATE_TIME -> 2022-04-25T22:49:08.1764754
DateTimeFormatter.ISO_OFFSET_DATE_TIME -> 2022-04-25T22:49:08.1764754+02:00
DateTimeFormatter.ISO_ZONED_DATE_TIME -> 2022-04-25T22:49:08.1764754+02:00[Europe/Paris]
DateTimeFormatter.ISO_DATE_TIME -> 2022-04-25T22:49:08.1764754+02:00[Europe/Paris]
DateTimeFormatter.ISO_ORDINAL_DATE -> 2022-115+02:00
DateTimeFormatter.ISO_WEEK_DATE -> 2022-W17-1+02:00
DateTimeFormatter.ISO_INSTANT -> 2022-04-25T20:49:08.176475400Z
DateTimeFormatter.BASIC_ISO_DATE -> 20220425+0200
DateTimeFormatter.RFC_1123_DATE_TIME -> Mon, 25 Apr 2022 22:49:08 +0200
Convert ZonedDateTime to String using toString() method
In Java we can simply use the ZonedDateTime.toString() method to convert a ZonedDateTime object to String as the example Java code below.
ConvertZonedDateTimeToStringExample7.java
import java.time.ZonedDateTime;
public class ConvertZonedDateTimeToStringExample7 {
public static void main(String... args) {
ZonedDateTime zonedDateTime = ZonedDateTime.now();
String zonedDateTimeString = zonedDateTime.toString();
System.out.println(zonedDateTimeString);
}
}
2022-05-07T15:03:19.518928300+07:00[Asia/Bangkok]
Happy Coding 😊
Related Articles
Java Convert ZonedDateTime to Instant
Java Convert ZonedDateTime to Date
Java Convert ZonedDateTime to Calendar
Java Convert ZonedDateTime to LocalDateTime
Java Convert ZonedDateTime to OffsetDateTime
Java Convert ZonedDateTime to LocalTime
Java Convert ZonedDateTime to LocalDate
Java Convert ZonedDateTime to ZoneOffset
Java Convert ZonedDateTime to ZoneId