LocalTime format() method in Java with Examples
The format() method of a LocalTime class is used to format this time using the specified formatter passed as a parameter. This method formats this time based on passed formatter to a string.
Syntax:
public String format(DateTimeFormatter formatter)
Parameters: This method accepts a single parameter formatter which is the specified formatter. It should not be null.
Return value: This method returns the formatted 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 // LocalTime.format() method import java.time.*; import java.time.format.DateTimeFormatter; public class GFG { public static void main(String[] args) { // create a LocalTime Objects LocalTime time = LocalTime.parse("03:18:23"); // create formatter Object DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME; // apply format String value = time.format(formatter); // print result System.out.println("value : " + value); } } |
value : 03:18:23
Program 2:
// Java program to demonstrate // LocalTime.format() method import java.time.*; import java.time.format.DateTimeFormatter; public class GFG { public static void main(String[] args) { // create a LocalTime Objects LocalTime time = LocalTime.parse("23:59:59"); // create formatter Object for ISO_LOCAL_TIME DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME; // apply format String value = time.format(formatter); // print result System.out.println("value : " + value); } } |
value : 23:59:59
Recommended Posts:
- LocalTime with() Method in Java with Examples
- LocalTime get() method in Java with Examples
- LocalTime from() method in Java with Examples
- LocalTime until() Method in Java with Examples
- LocalTime plus() method in Java with Examples
- LocalTime ofNanoOfDay() method in Java with Examples
- LocalTime truncatedTo() method in Java with Examples
- LocalTime withNano() method in Java with Examples
- LocalTime atOffset() method in Java with Examples
- LocalTime hashCode() method in Java with Examples
- LocalTime adjustInto() method in Java with Examples
- LocalTime equals() method in Java with Examples
- LocalTime compareTo() method in Java with Examples
- LocalTime getHour() method in Java with Examples
- LocalTime atDate() 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.



