Java Duration Class
The Duration class is an important class in the Date and Time API (java.time package) introduced in Java 8, used to represent a time interval or duration.
The Duration class is mainly used to calculate the difference between two points in time, or to represent a specific length of time.
Basic concepts
The time interval represented by the Duration class can be accurate to the nanosecond level. It is mainly used to handle time-based amounts (hours, minutes, seconds, and nanoseconds), without involving the concept of dates or time zones.
Main features:
- Immutability - A Duration object cannot be modified once created
- Thread safety - Can be safely used in a multithreaded environment
- High precision - Can be accurate to the nanosecond level
- Method chaining - Supports method chaining
Creating a Duration Object
The Duration class provides a variety of static methods to create Duration instances:
1. Created via time units
Example
// Create a Duration lasting 2 hours
Duration twoHours
=
Duration.
ofHours
(
2
)
;
// Create a Duration lasting 30 minutes
Duration thirtyMinutes
=
Duration.
ofMinutes
(
30
)
;
// Create a Duration lasting 45 seconds
Duration fortyFiveSeconds
=
Duration.
ofSeconds
(
45
)
;
// Create a Duration lasting 500 milliseconds
Duration fiveHundredMillis
=
Duration.
ofMillis
(
500
)
;
// Create a Duration lasting 1,000,000 nanoseconds
Duration oneMillionNanos
=
Duration.
ofNanos
(
1
_000_000
)
;
2. Created via amounts of time
Example
// Create a Duration lasting 1 day, 2 hours, and 3 minutes
Duration customDuration
=
Duration.
ofDays
(
1
)
.
plusHours
(
2
)
.
plusMinutes
(
3
)
;
3. Calculated from two points in time
Example
Instant start
=
Instant.
now
(
)
;
// Perform some operations...
Instant end
=
Instant.
now
(
)
;
Duration elapsed
=
Duration.
between
(
start, end
)
;
4. Created by parsing a string
Example
// Create from an ISO-8601 format string
Duration parsedDuration
=
Duration.
parse
(
"PT2H30M15S"
)
;
// 2 hours, 30 minutes, and 15 seconds
// 2022-01-01 00:00:00 GMT
The Duration class provides a rich set of methods for handling time intervals:
1. Getting time values
Example
Duration duration
=
Duration.
ofHours
(
2
)
.
plusMinutes
(
30
)
;
long
hours
=
duration.
toHours
(
)
;
// 2
long
minutes
=
duration.
toMinutes
(
)
;
// 150
long
seconds
=
duration.
getSeconds
(
)
;
// 9000
int
nanos
=
duration.
getNano
(
)
;
// 0
2. Time arithmetic
Example
Duration d1
=
Duration.
ofHours
(
2
)
;
Duration d2
=
Duration.
ofMinutes
(
30
)
;
// Addition
Duration sum
=
d1.
plus
(
d2
)
;
// 2 hours and 30 minutes
// Subtraction
Duration diff
=
d1.
minus
(
d2
)
;
// 1 hour and 30 minutes
// Multiplication
Duration multiplied
=
d1.
multipliedBy
(
3
)
;
// 6 hours
// Division
Duration divided
=
d1.
dividedBy
(
2
)
;
// 1 hour
3. Comparison operations
Example
Duration d1
=
Duration.
ofHours
(
1
)
;
Duration d2
=
Duration.
ofMinutes
(
90
)
;
boolean
isEqual
=
d1.
equals
(
d2
)
;
// false
int
comparison
=
d1.
compareTo
(
d2
)
;
// -1 (d1 < d2)
boolean
isNegative
=
d1.
isNegative
(
)
;
// false
boolean
isZero
=
d1.
isZero
(
)
;
// false
4. Other utility methods
Example
Duration duration
=
Duration.
ofMinutes
(
90
)
;
// Convert to other units
long
hours
=
duration.
toHours
(
)
;
// 1
long
minutes
=
duration.
toMinutes
(
)
;
// 90
long
seconds
=
duration.
toSeconds
(
)
;
// 5400
long
millis
=
duration.
toMillis
(
)
;
// 5400000
long
nanos
=
duration.
toNanos
(
)
;
// 5400000000000
// Get each part
long
secondsPart
=
duration.
getSeconds
(
)
;
// 5400
int
nanoPart
=
duration.
getNano
(
)
;
// 0
// Take the absolute value
Duration absDuration
=
duration.
abs
(
)
;
// Take the negative value
Duration negated
=
duration.
negated
(
)
;
Practical Application Examples
Example 1: Calculating Code Execution Time
Example
Instant start
=
Instant.
now
(
)
;
// Simulate a time-consuming operation
try
{
Thread
.
sleep
(
1500
)
;
}
catch
(
InterruptedException
e
)
{
e.
printStackTrace
(
)
;
}
Instant end
=
Instant.
now
(
)
;
Duration elapsed
=
Duration.
between
(
start, end
)
;
System
.
out
.
println
(
"Code execution took: "
elapsed.
toMillis
(
)
" ms"
)
;
Example 2: Time Format Conversion
Example
Duration duration
=
Duration.
ofMinutes
(
135
)
;
// Convert to "HH:MM:SS" format
long
hours
=
duration.
toHours
(
)
;
long
minutes
=
duration.
minusHours
(
hours
)
.
toMinutes
(
)
;
long
seconds
=
duration.
minusHours
(
hours
)
.
minusMinutes
(
minutes
)
.
getSeconds
(
)
;
String
formatted
=
String
.
format
(
"%02d:%02d:%02d"
, hours, minutes, seconds
)
;
System
.
out
.
println
(
formatted
)
;
// Output: 02:15:00
Example 3: Timeout Control
Example
Duration timeout
=
Duration.
ofSeconds
(
30
)
;
Instant startTime
=
Instant.
now
(
)
;
while
(
true
)
{
// Check for timeout
if
(
Duration.
between
(
startTime, Instant.
now
(
)
)
.
compareTo
(
timeout
)
0
)
{
System
.
out
.
println
(
"Operation timed out"
)
;
break
;
}
// Perform other operations...
}
Example
import
java.time.Duration
;
import
java.time.LocalTime
;
import
java.time.Instant
;
public
class
DurationDemo
{
public
static
void
main
(
String
[
]
args
)
{
// Several ways to create Duration objects
// 1. Use the of...() methods
Duration duration1
=
Duration.
ofHours
(
2
)
;
// 2 hours
Duration duration2
=
Duration.
ofMinutes
(
90
)
;
// 90 minutes
System
.
out
.
println
(
"Duration 1: "
duration1
)
;
System
.
out
.
println
(
"Duration 2: "
duration2
)
;
// 2. Use the between() method to calculate the duration between two points in time
LocalTime startTime
=
LocalTime.
of
(
9
,
0
)
;
LocalTime endTime
=
LocalTime.
of
(
11
,
30
)
;
Duration duration3
=
Duration.
between
(
startTime, endTime
)
;
System
.
out
.
println
(
"Duration between times: "
duration3
)
;
// 3. Use parse() to create from a string
Duration duration4
=
Duration.
parse
(
"PT2H30M"
)
;
// ISO-8601 format
System
.
out
.
println
(
"Parsed Duration: "
duration4
)
;
// 4. Use Instant to calculate the time difference
Instant startInstant
=
Instant.
now
(
)
;
// Simulate a time-consuming operation
try
{
Thread
.
sleep
(
1500
)
;
}
catch
(
InterruptedException
e
)
{
}
Instant endInstant
=
Instant.
now
(
)
;
Duration duration5
=
Duration.
between
(
startInstant, endInstant
)
;
System
.
out
.
println
(
"Execution duration: "
duration5.
toMillis
(
)
"ms"
)
;
}
}
The output is:
Duration 1: PT2H
Duration 2: PT1H30M
Duration between times: PT2H30M
Parsed Duration: PT2H30M
Execution duration: 1501ms
Notes
-
Precision Issues: Duration can be accurate to the nanosecond level, but the actual precision depends on the support of the operating system and hardware.
-
Difference from Period:
-
Immutability: All Duration objects are immutable; any modification operation returns a new Duration instance.
-
Negative Value Handling: Duration can represent negative time intervals, which may be encountered when calculating time differences.
-
String Format: Uses the ISO-8601 duration format
PnDTnHnMn.nS, where:
Summary
Java's Duration class provides a powerful and flexible tool for handling time intervals. By mastering the use of the Duration class, developers can more easily handle various time-calculation-related needs, such as performance measurement, timeout control, and task scheduling. The Duration class works together with other date-time classes introduced in Java 8 (such as Instant and LocalDateTime) to build more robust and readable time-handling code.