To work with Java's Duration
class, you need to import the java.time.Duration
package. You can do so by adding the following import statement at the beginning of your Java file:
1import java.time.Duration;
To create a Duration
object, you can utilize one of the several available factory methods. The most common way is by specifying the amount of time in seconds, such as:
1Duration duration = Duration.ofSeconds(60);
This creates a Duration
object representing 60 seconds. You can also use other time units like minutes, hours, or days by utilizing the appropriate factory methods, such as ofMinutes
, ofHours
, or ofDays
.
Another way to create a Duration
object is by specifying the amount of time using a combination of different time units. For example, if you want to represent 2 hours, 30 minutes, and 45 seconds, you can use the of
method as follows:
1Duration duration = Duration.ofHours(2).plusMinutes(30).plusSeconds(45);
Once you have a Duration
object, you can perform various operations and calculations with it. Some of the commonly used methods include:
toDays()
, toHours()
, toMinutes()
, toSeconds()
: These methods allow you to retrieve the equivalent time units of the duration.
plus(Duration)
, minus(Duration)
: These methods enable you to add or subtract another duration from the current one.
multipliedBy(long)
, dividedBy(long)
: These methods let you multiply or divide the duration by a scalar value.
compareTo(Duration)
: This method compares the current duration with another duration and returns an integer indicating their relative order.
Here's an example that demonstrates some of these operations:
1Duration duration1 = Duration.ofMinutes(90);2Duration duration2 = Duration.ofHours(2);3
4long totalMinutes = duration1.plus(duration2).toMinutes();5long totalSeconds = duration2.minus(duration1).toSeconds();6
7System.out.println("Total minutes: " + totalMinutes); // Output: Total minutes: 2108System.out.println("Total seconds: " + totalSeconds); // Output: Total seconds: 1800
In this example, we add duration2
to duration1
and retrieve the total minutes. We also subtract duration1
from duration2
and get the total seconds.
Java's Duration
class also provides a convenient way to parse durations from textual representations. You can use the parse
method to parse a duration string in the ISO-8601 format, which consists of a number followed by a time unit symbol.
1Duration duration = Duration.parse("PT3H30M");
This creates a Duration
object representing 3 hours and 30 minutes. The "PT"
prefix denotes a duration in the ISO-8601 format, followed by the hours "3H"
and the minutes "30M"
.
If you need to convert a Duration
object to a different time representation, such as LocalTime
or Instant
, you can achieve that by utilizing the to
method.
For instance, to convert a duration to seconds as a long
value, you can do:
1long totalSeconds = duration.toSeconds();
Java's Duration
class provides a comprehensive set of functionalities for working with durations and performing time-based calculations. By importing the java.time.Duration
package and utilizing the Duration
class, you can handle durations effectively in your Java applications.