LocalTime parse() method in Java with Examples
In LocalTime class, there are two types of parse() method depending upon the parameters passed to it.
parse(CharSequence text)
parse() method of a LocalTime class used to get an instance of LocalTime from a string such as ’10:15:45′ passed as parameter.The string must have a valid date-time and is parsed using DateTimeFormatter.ISO_LOCAL_TIME.
Syntax:
public static LocalTime parse(CharSequence text)
Parameters: This method accepts only one parameter text which is the text to parse in LocalTime. It should not be null.
Return value: This method returns LocalTime which is the parsed local date-time.
Exception: This method throws DateTimeParseException if the text cannot be parsed.
Below programs illustrate the parse() method:
Program 1:
// Java program to demonstrate // LocalTime.parse() method import java.time.*; public class GFG { public static void main(String[] args) { // create an LocalTime object LocalTime lt = LocalTime.parse("10:15:45"); // print result System.out.println("LocalTime : " + lt); } } |
LocalTime : 10:15:45
parse(CharSequence text, DateTimeFormatter formatter)
parse() method of a LocalTime class used to get an instance of LocalTime from a string such as ’10:15:45′ passed as parameter using a specific formatter.The date-time is parsed using a specific formatter.
Syntax:
public static LocalTime parse(CharSequence text,
DateTimeFormatter formatter)
Parameters: This method accepts two parameters text which is the text to parse and formatter which is the formatter to use.
Return value: This method returns LocalTime which is the parsed local date-time.
Exception: This method throws DateTimeParseException if the text cannot be parsed.
Below programs illustrate the parse() method:
Program 1:
// Java program to demonstrate // LocalTime.parse() method import java.time.*; import java.time.format.*; public class GFG { public static void main(String[] args) { // create a formater DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME; // create an LocalTime object and LocalTime lt = LocalTime .parse("10:15:45", formatter); // print result System.out.println("LocalTime : " + lt); } } |
LocalTime : 10:15:45
References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#parse(java.lang.CharSequence)
https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#parse(java.lang.CharSequence, java.time.format.DateTimeFormatter)
Recommended Posts:
- LocalTime get() method in Java with Examples
- LocalTime with() Method in Java with Examples
- LocalTime from() method in Java with Examples
- LocalTime plus() method in Java with Examples
- LocalTime until() Method in Java with Examples
- Instant parse() method in Java with Examples
- Period parse() method in Java with Examples
- SimpleDateFormat parse() Method in Java with Examples
- OffsetTime parse() method in Java with examples
- ZonedDateTime parse() method in Java with Examples
- LocalDateTime parse() method in Java with Examples
- NumberFormat parse() method in Java with Examples
- LocalDate parse() method in Java with Examples
- LocalTime format() method in Java with Examples
- LocalTime minus() 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.



