OffsetTime parse() method in Java with examples
In OffsetTime class, there are two types of parse() method depending upon the parameters passed to it.
parse(CharSequence text)
parse() method of a OffsetTime class used to get an instance of OffsetTime from a string such as ’15:25:10+01:00′ passed as parameter.The string must have a valid date-time and is parsed using DateTimeFormatter.ISO_LOCAL_TIME.
Syntax:
public static OffsetTime parse(CharSequence text)
Parameters: This method accepts only one parameter text which is the text to parse in OffsetTime. It should not be null.
Return value: This method returns OffsetTime 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 // OffsetTime.parse() method import java.time.*; public class GFG { public static void main(String[] args) { // create an OffsetTime object OffsetTime lt = OffsetTime.parse("15:25:10+01:00"); // print result System.out.println("OffsetTime : " + lt); } } |
OffsetTime : 15:25:10+01:00
parse(CharSequence text, DateTimeFormatter formatter)
parse() method of a OffsetTime class used to get an instance of OffsetTime from a string such as ’15:25:10+01:00′ passed as parameter using a specific formatter.The date-time is parsed using a specific formatter.
Syntax:
public static OffsetTime 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 OffsetTime 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 // OffsetTime.parse() method import java.time.*; import java.time.format.DateTimeFormatter; public class GFG { public static void main(String[] args) { DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_OFFSET_TIME; // create an OffsetTime object and OffsetTime lt = OffsetTime .parse("11:35:34+01:00", dateTimeFormatter); // print result System.out.println("OffsetTime : " + lt); } } |
OffsetTime : 11:35:34+01:00
References: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#parse-java.lang.CharSequence-
Recommended Posts:
- OffsetTime get() method in Java with examples
- OffsetTime from() method in Java with examples
- OffsetTime until() Method in Java with Examples
- OffsetTime with() Method in Java with Examples
- OffsetTime plus() method in Java with examples
- OffsetTime now() method in Java with examples
- OffsetTime format() method in Java with examples
- OffsetTime isEqual() method in Java with examples
- OffsetTime toLocalTime() method in Java with examples
- OffsetTime isAfter() method in Java with examples
- OffsetTime isBefore() method in Java with examples
- OffsetTime compareTo() method in Java with examples
- OffsetTime toString() method in Java with examples
- OffsetTime query() method in Java with examples
- OffsetTime plusMinutes() 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.

