Duration toString() example
Description
Duration toString()
returns a string representation of
this duration using ISO-8601 seconds based representation, such as PT8H6M12.345S.
The format of the string will be PTnHnMnS
, where
n is the relevant hours, minutes or seconds part of the duration.
Any fractional seconds are placed after a decimal point i the seconds section.
If a section has a zero value, it is omitted. The hours, minutes and seconds will all have the same sign.
Examples:
"20.345 seconds" -- "PT20.345S
"15 minutes" (15 * 60 seconds) -- "PT15M"
"10 hours" (10 * 3600 seconds) -- "PT10H"
"2 days" (2 * 86400 seconds) -- "PT48H"
Syntax
toString
has the following syntax.
public String toString()
Example
The following example shows how to use toString
.
import java.time.Duration;
import java.time.LocalTime;
/* w w w .j a v a2s. c o m*/
public class Main {
public static void main(String[] args) {
Duration duration = Duration.between(LocalTime.MIDNIGHT,LocalTime.NOON);
System.out.println(duration.getSeconds());
System.out.println(duration.toString());
}
}
The code above generates the following result.