The java.time.Duration
class models a quantity or amount of time in terms of seconds and nanoseconds.
Java Duration Methods
The table below contains various methods of the Java Duration class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.
Method | Description |
---|---|
abs() | Returns a copy of this duration with a positive length. |
between() | Obtains a Duration representing the duration between two temporal objects. |
compareTo() | Compares this duration to another duration. |
dividedBy() | Returns a copy of this duration divided by the specified value. |
equals() | Checks if this duration is equal to another duration. |
get() | Gets the value of the requested unit from this duration. |
getNano() | Gets the number of nanoseconds within the second of this duration. |
getSeconds() | Gets the number of seconds in this duration. |
isNegative() | Checks if this duration is negative. |
minus() | Returns a copy of this duration with the specified duration subtracted. |
minusDays() | Returns a copy of this duration with the specified number of days subtracted. |
minusHours() | Returns a copy of this duration with the specified number of hours subtracted. |
minusMillis() | Returns a copy of this duration with the specified number of milliseconds subtracted. |
minusMinutes() | Returns a copy of this duration with the specified number of minutes subtracted. |
minusSeconds() | Returns a copy of this duration with the specified number of seconds subtracted. |
of() | Obtains a Duration representing a number of seconds and nanoseconds. |
ofDays() | Obtains a Duration representing a number of standard 24-hour days. |
ofHours() | Obtains a Duration representing a number of standard hours. |
ofMillis() | Obtains a Duration representing a number of milliseconds. |
ofSeconds() | Obtains a Duration representing a number of seconds. |
parse() | Obtains a Duration from a text string such as PT8H6M12S . |
plus() | Returns a copy of this duration with the specified duration added. |
plusDays() | Returns a copy of this duration with the specified number of days added. |
plusHours() | Returns a copy of this duration with the specified number of hours added. |
plusMillis() | Returns a copy of this duration with the specified number of milliseconds added. |
plusMinutes() | Returns a copy of this duration with the specified number of minutes added. |
plusSeconds() | Returns a copy of this duration with the specified number of seconds added. |
toDays() | Gets the number of days in this duration. |
toHours() | Gets the number of hours in this duration. |
toMillis() | Gets the number of milliseconds in this duration. |
toMinutes() | Gets the number of minutes in this duration. |
toSeconds() | Gets the number of seconds in this duration. |
In conclusion, the java.time.Duration
class provides various methods to work with durations, representing amounts of time. These methods help in performing operations such as adding, subtracting, and comparing durations, as well as converting them to different units. For more detailed information, refer to the official Java Documentation.