OffsetTime format() method in Java with examples

The format() method of OffsetTime class in Java formats this time using the specified formatter which is passed in the parameter of the function.

Syntax:

public String format(DateTimeFormatter formatter)

Parameter: This method accepts a single mandatory parameter formatter which specifies the formatter to be used and it is not null.

Return Value: It returns the formatted date string and it is not null.

Exceptions: The function returns DateTimeException which is thrown by the method when an error occurs during printing.



Below programs illustrate the format() method:

Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the format() method
  
import java.time.OffsetTime;
import java.time.format.DateTimeFormatter;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Parses the given time
        OffsetTime time
            = OffsetTime.parse("15:45:35+06:02");
  
        // Prints the parsed time
        System.out.println("Time: "
                           + time);
  
        // Function used
        DateTimeFormatter formatter
            = DateTimeFormatter.ISO_TIME;
  
        // Prints the formatted time
        System.out.println("Formatted time: "
                           + formatter.format(time));
    }
}

chevron_right


Output:

Time: 15:45:35+06:02
Formatted time: 15:45:35+06:02

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the format() method
  
import java.time.OffsetTime;
import java.time.format.DateTimeFormatter;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Parses the given time
        OffsetTime time
            = OffsetTime.parse("11:14:13+07:05");
  
        // Prints the parsed time
        System.out.println("Time: "
                           + time);
  
        // Function used
        DateTimeFormatter formatter
            = DateTimeFormatter.ISO_TIME;
  
        // Prints the formatted time
        System.out.println("Formatted time: "
                           + formatter.format(time));
    }
}

chevron_right


Output:

Time: 11:14:13+07:05
Formatted time: 11:14:13+07:05

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#format-java.time.format.DateTimeFormatter-

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.