Duration parse(CharSequence) method in Java with Examples
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 “nDT” means ‘n’ number of Days, “nH” means ‘n’ number of Hours, “nM” means ‘n’ number of Minutes and “nS” means ‘n’ number of Seconds. The formats accepted are based on the ISO-8601 duration format.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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 2: To demonstrate DateTimeParseException
Exception: java.time.format.DateTimeParseException: Text cannot be parsed to a Duration