Java ZonedDateTime.format() Method with Examples

Tags: Java ZonedDateTime Java 8 DateTimeFormatter

In this Java core tutorial we learn how to use the java.time.ZonedDateTime.format(DateTimeFormatter formatter) method to convert a ZonedDateTime object to a String in Java programming language.

How to use ZonedDateTime.format() method

In Java with a given ZonedDateTime object we can use the ZonedDateTime.format(DateTimeFormatter formatter) method to convert it to a date time String in specified format.

For example, in the following Java program we use format() method with custom date time format patterns to convert ZonedDateTime object to a specified date time format String.

ZonedDateTimeFormatExample1.java

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeFormatExample1 {
    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-MM-yyyy HH:mm:ss X");
        String zonedDateTimeString2 = zonedDateTime.format(dateTimeFormatter2);

        DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss VV");
        String zonedDateTimeString3 = zonedDateTime.format(dateTimeFormatter3);

        DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss O");
        String zonedDateTimeString4 = zonedDateTime.format(dateTimeFormatter4);

        System.out.println("dd-MM-yyyy HH:mm:ss Z -> " + zonedDateTimeString1);
        System.out.println("dd-MM-yyyy HH:mm:ss X -> " + zonedDateTimeString2);
        System.out.println("dd-MM-yyyy HH:mm:ss VV -> " + zonedDateTimeString3);
        System.out.println("dd-MM-yyyy HH:mm:ss O -> " + zonedDateTimeString4);
    }
}
The output as below.
dd-MM-yyyy HH:mm:ss Z -> 05-05-2022 22:26:19 +0700
dd-MM-yyyy HH:mm:ss X -> 05-05-2022 22:26:19 +07
dd-MM-yyyy HH:mm:ss VV -> 05-05-2022 22:26:19 Asia/Bangkok
dd-MM-yyyy HH:mm:ss O -> 05-05-2022 22:26:19 GMT+7

We also can use format() method with predefined DateTimeFormatter constants as below.

ZonedDateTimeFormatExample2.java

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeFormatExample2 {
    public static void main(String... args) {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
        String zonedDateTimeString = zonedDateTime.format(dateTimeFormatter);

        System.out.println(zonedDateTimeString);
    }
}
The output as below.
2022-05-05T22:26:46.2664598+07:00[Asia/Bangkok]

Happy Coding 😊

Java ZonedDateTime.now() method with Examples

Java ZonedDateTime.of() Method with Examples

Java ZonedDateTime.ofInstant() Method with Examples

Java ZonedDateTime.ofStrict() Method with Examples

Java ZonedDateTime.parse() Method with Examples

Java ZonedDateTime.getOffset() Method with Examples

Java ZonedDateTime.withEarlierOffsetAtOverlap() Method with Examples

Java ZonedDateTime.withLaterOffsetAtOverlap() Method with Examples

Java ZonedDateTime.getZone() Method with Examples

Java ZonedDateTime.withZoneSameLocal() Method with Examples

Java ZonedDateTime.withZoneSameInstant() Method with Examples

Java ZonedDateTime.withFixedOffsetZone() Method with Examples

Java ZonedDateTime.toLocalDateTime() Method with Examples

Java ZonedDateTime.toLocalDate() Method with Examples

Java ZonedDateTime.toLocalTime() Method with Examples

Java ZonedDateTime.getYear() Method with Examples

Java ZonedDateTime.getMonthValue() Method with Examples

Java ZonedDateTime.getMonth() Method with Examples

Java ZonedDateTime.getDayOfMonth() Method with Examples

Java ZonedDateTime.getDayOfYear() Method with Examples

Java ZonedDateTime.getDayOfWeek() Method with Examples

Java ZonedDateTime.getHour() Method with Examples

Java ZonedDateTime.getMinute() Method with Examples

Java ZonedDateTime.getSecond() Method with Examples

Java ZonedDateTime.getNano() Method with Examples

Java ZonedDateTime.withYear() Method with Examples

Java ZonedDateTime.withMonth() Method with Examples

Java ZonedDateTime.withDayOfMonth() Method with Examples

Java ZonedDateTime.withDayOfYear() Method with Examples

Java ZonedDateTime.withHour() Method with Examples

Java ZonedDateTime.withMinute() Method with Examples

Java ZonedDateTime.withSecond() Method with Examples

Java ZonedDateTime.withNano() Method with Examples

Java ZonedDateTime.plusYears() Method with Examples

Java ZonedDateTime.plusMonths() Method with Examples

Java ZonedDateTime.plusWeeks() Method with Examples

Java ZonedDateTime.plusDays() Method with Examples

Java ZonedDateTime.plusHours() Method with Examples

Java ZonedDateTime.plusMinutes() Method with Examples

Java ZonedDateTime.plusSeconds() Method with Examples

Java ZonedDateTime.plusNanos() Method with Examples

Java ZonedDateTime.minusYears() Method with Examples

Java ZonedDateTime.minusMonths() Method with Examples

Java ZonedDateTime.minusWeeks() Method with Examples

Java ZonedDateTime.minusDays() Method with Examples

Java ZonedDateTime.minusHours() Method with Examples

Java ZonedDateTime.minusMinutes() Method with Examples

Java ZonedDateTime.minusSeconds() Method with Examples

Java ZonedDateTime.minusNanos() Method with Examples

Java ZonedDateTime.toOffsetDateTime() Method with Examples

Java ZonedDateTime.equals() Method with Examples

Java ZonedDateTime.toString() Method with Examples

Java Compare two ZonedDateTime Values

Java Convert LocalDateTime to ZonedDateTime

Java Convert LocalDate to ZonedDateTime