Duration parse(CharSequence) method in Java with Examples Last Updated : 17 Oct, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report The parse(CharSequence) method of Duration Class in java.time package is used to get a Duration from a string passed as the parameter. The format for the String to be parsed is "PnDTnHnMn.nS" where "nD" means 'n' number of Days, "nH" means 'n' number of Hours, "nM" means 'n' number of Minutes, "nS" means 'n' number of Seconds and "T" is a prefix that must be used before the part consisting of "nHnMn.nS". The formats accepted are based on the ISO-8601 duration format. Syntax: public static Duration parse(CharSequence text) Parameters: This method accepts a parameter text which is CharSequence to be parsed into Duration. Return Value: This method returns a Duration representing the time passed in the form of CharSequence as parameter. Exception: This method throws DateTimeParseException if the text cannot be parsed to a duration. Below examples illustrate the Duration.parse() method: Example 1: Java // Java code to illustrate parse() method import java.time.Duration; public class GFG { public static void main(String[] args) { // Get the text String time = "P2DT3H4M"; // Duration using parse() method Duration duration = Duration.parse(time); System.out.println(duration.getSeconds()); } } Output:183840 Example 2: To demonstrate DateTimeParseException Java // Java code to illustrate parse() method import java.time.Duration; public class GFG { public static void main(String[] args) { // Get the text String time = "M"; try { // Duration using parse() method Duration duration = Duration.parse(time); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output:Exception: java.time.format.DateTimeParseException: Text cannot be parsed to a Duration Reference: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#parse-java.lang.CharSequence- Comment More infoAdvertise with us Next Article Duration parse(CharSequence) method in Java with Examples C code_r Follow Improve Article Tags : Java Java-Functions Java-time package Java-Duration Practice Tags : Java Similar Reads MonthDay parse(CharSequence) method in Java with Examples The parse(CharSequence text) method of the MonthDay class in Java is used to get an instance of MonthDay from a text string. Syntax: public static MonthDay parse( CharSequence text) Parameters: This method accepts text as parameter to parse. Return value: This method returns the parsed month-day. Ex 1 min read Pattern split(CharSequence) method in Java with Examples split(CharSequence) method of a Pattern class used to splits the given char sequence passed as parameter to method around matches of this pattern.This method can split charSequence into an array of String's, using the regular expression used to compile the pattern as a delimiter.so we can say that t 2 min read Duration abs() method in Java with Examples The abs() method of Duration Class in java.time package is used to get an immutable copy of this duration with the absolute duration. Syntax: public Duration abs() Parameters: This method do not accepts any parameter. Return Value: This method returns a Duration which is an immutable copy of the exi 1 min read Duration plus(Duration) method in Java with Examples The plus(Duration) method of Duration Class in java.time package is used to get an immutable copy of this duration with the specified duration added, passed as the parameter. Syntax: public Duration plus(Duration duration) Parameters: This method accepts a parameter duration which is the duration to 2 min read Duration toDays() method in Java with Examples The toDays() method of Duration Class in java.time package is used get the value of this duration in number of days. Syntax: public long toDays() Parameters: This method do not accepts any parameter. Return Value: This method returns a long value which is the number of days in this duration. Below e 1 min read Like