Duration parse(CharSequence text)
gets a Duration from
a text string such as PnDTnHnMn.nS.
The formats accepted are based on the ISO-8601 duration format PnDTnHnMn.nS with days considered to be exactly 24 hours.
The string starts with an optional sign. If negative, the whole period is negated.
The suffixes in ASCII of "D", "H", "M" and "S" mark days, hours, minutes and seconds, accepted in upper or lower case.
Examples:
"PT21.345S" -- parses as "21.345 seconds" "PT11M" -- parses as "11 minutes" (where a minute is 60 seconds) "PT11H" -- parses as "11 hours" (where an hour is 3600 seconds) "P3D" -- parses as "3 days" (where a day is 24 hours or 86400 seconds) "P3DT3H4M" -- parses as "3 days, 3 hours and 4 minutes" "P-7H3M" -- parses as "-7 hours and +3 minutes" "-P7H3M" -- parses as "-7 hours and -3 minutes" "-P-7H+3M" -- parses as "+7 hours and -3 minutes"
parse
has the following syntax.
public static Duration parse(CharSequence text)
The following example shows how to use parse
.
import java.time.Duration; /* w w w. ja v a 2 s . c o m*/ public class Main { public static void main(String[] args) { Duration duration = Duration.parse("PT20.345S"); System.out.println(duration); duration = Duration.parse("PT15M"); System.out.println(duration); duration = Duration.parse("PT10H"); System.out.println(duration); duration = Duration.parse("P2D"); System.out.println(duration); duration = Duration.parse("P2DT3H4M"); System.out.println(duration); duration = Duration.parse("P2DT3H4M"); System.out.println(duration); duration = Duration.parse("P-6H3M"); System.out.println(duration); duration = Duration.parse("-P6H3M"); System.out.println(duration); duration = Duration.parse("-P-6H+3M"); System.out.println(duration); } }
The code above generates the following result.