LocalTime format() method in Java with Examples
Last Updated :
03 Dec, 2018
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:
import java.time.*;
import java.time.format.DateTimeFormatter;
public class GFG {
public static void main(String[] args)
{
LocalTime time
= LocalTime.parse( "03:18:23" );
DateTimeFormatter formatter
= DateTimeFormatter.ISO_TIME;
String value = time.format(formatter);
System.out.println( "value : "
+ value);
}
}
|
Program 2:
import java.time.*;
import java.time.format.DateTimeFormatter;
public class GFG {
public static void main(String[] args)
{
LocalTime time
= LocalTime.parse( "23:59:59" );
DateTimeFormatter formatter
= DateTimeFormatter.ISO_LOCAL_TIME;
String value = time.format(formatter);
System.out.println( "value : "
+ value);
}
}
|
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#format(java.time.format.DateTimeFormatter)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...