Durations
Description
Duration
object represents the time span between two instants.
The Duration class can have both positive and negative value.
Example
We can create the Duration class using one of its ofXXX() static factory methods.
import java.time.Duration;
/*from w w w . java2 s . co m*/
public class Main {
public static void main(String[] args) {
Duration d1 = Duration.ofDays(2);
System.out.println(d1);
Duration d2 = Duration.ofMinutes(2);
System.out.println(d2);
}
}
The code above generates the following result.
Example 2
Multiplication, division, and negation are applicable to Duration objects.
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 d = Duration.ofSeconds(200); // 3 minutes and 20 seconds
Duration d1 = d.multipliedBy(2); // 6 minutes and 40 seconds
Duration d2 = d.negated(); // -3 minutes and -20 seconds
System.out.println(d);
System.out.println(d1);
System.out.println(d2);
}
}
The code above generates the following result.