The format() method of ChronoZonedDateTime interface 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:
default 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 // ChronoZonedDateTime.format() method import java.time.*; import java.time.chrono.*; import java.time.format.*; public class GFG { public static void main(String[] args) { // create ChronoZonedDateTime objects ChronoZonedDateTime 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); } } |
Result: 19:21:12.123+05:30
Program 2:
// Java program to demonstrate // ChronoZonedDateTime.format() method import java.time.*; import java.time.chrono.*; import java.time.format.*; public class GFG { public static void main(String[] args) { // create ChronoZonedDateTime objects ChronoZonedDateTime 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); } } |
Result: 20181025+0200
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- ChronoZonedDateTime from() method in Java with Examples
- ChronoZonedDateTime until() method in Java with Examples
- ChronoZonedDateTime get() method in Java with Examples
- ChronoZonedDateTime withLaterOffsetAtOverlap() method in Java with Examples
- ChronoZonedDateTime equals() method in Java with Examples
- ChronoZonedDateTime withZoneSameInstant() method in Java with Examples
- ChronoZonedDateTime toString() method in Java with Examples
- ChronoZonedDateTime withEarlierOffsetAtOverlap() method in Java with Examples
- ChronoZonedDateTime with(TemporalAdjuster) method in Java with Examples
- ChronoZonedDateTime withZoneSameLocal() method in Java with Examples
- ChronoZonedDateTime range() method in Java with Examples
- ChronoZonedDateTime plus(TemporalAmount) method in Java with Examples
- ChronoZonedDateTime toLocalDate() method in Java with Examples
- ChronoZonedDateTime toInstant() method in Java with Examples
- ChronoZonedDateTime toLocalTime() method in Java with Examples
- ChronoZonedDateTime getChronology() method in Java with Examples
- ChronoZonedDateTime getZone() method in Java with Examples
- ChronoZonedDateTime isEqual() method in Java with Examples
- ChronoZonedDateTime getLong() method in Java with Examples
- ChronoZonedDateTime getOffset() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

