Saturday, September 6, 2025
HomeLanguagesJavaZonedDateTime format() method in Java with Examples

ZonedDateTime format() method in Java with Examples

The format() method of ZonedDateTime class in Java is used to format this date-time using the specified formatter passed as parameter.This date-time will be passed to the formatter to produce a string.

Syntax:

public String format(DateTimeFormatter formatter)

Parameters: This method accepts a single parameter formatter which represents the formatter to use. This is a mandatory parameter and should not be NULL.

Return value: This method returns a String represents the formatted date-time string.

Exception: This method throws a DateTimeException if an error occurs during printing.

Below programs illustrate the format() method:
Program 1:




// Java program to demonstrate
// ZonedDateTime.format() method
  
import java.time.*;
import java.time.format.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create ZonedDateTime objects
        ZonedDateTime zoneddatetime
            = ZonedDateTime.parse("2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
  
        // create a formatter
        DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME;
  
        // apply format()
        String value = zoneddatetime.format(formatter);
  
        // print result
        System.out.println("Result: " + value);
    }
}


Output:

Result: 19:21:12.123+05:30

Program 2:




// Java program to demonstrate
// ZonedDateTime.format() method
  
import java.time.*;
import java.time.format.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create ZonedDateTime objects
        ZonedDateTime zoneddatetime
            = ZonedDateTime.parse("2018-10-25T23:12:31.123+02:00[Europe/Paris]");
  
        // create a formatter
        DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
  
        // apply format()
        String value = zoneddatetime.format(formatter);
  
        // print result
        System.out.println("Result: " + value);
    }
}


Output:

Result: 20181025+0200

Reference:
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#format(java.time.format.DateTimeFormatter)

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS