Top Description Fields Constructors Methods
org.apache.commons.lang3.time

public Class DurationUtils

extends Object
Class Inheritance
Imports
java.time.Duration, .Instant, java.time.temporal.ChronoUnit, .Temporal, .TemporalUnit, java.util.Objects, java.util.concurrent.TimeUnit, org.apache.commons.lang3.LongRange, .ObjectUtils, .StringUtils, org.apache.commons.lang3.function.FailableBiConsumer, .FailableConsumer, .FailableRunnable, org.apache.commons.lang3.math.NumberUtils

Utilities for Duration.
Since
3.12.0

Field Summary

Modifier and TypeField and Description
pack-priv static final LongRange
LONG_TO_INT_RANGE

An Integer Range that accepts Longs.

Constructor Summary

AccessConstructor and Description
public
DurationUtils()
Deprecated TODO Make private in 4.0.

Make private in 4.0.

Method Summary

Modifier and TypeMethod and Description
public static <
The function exception.
T extends Throwable
>
void
accept(final FailableBiConsumer<Long, Integer, T>
Accepting function.
consumer
,
final Duration
The duration to pick apart.
duration
)

Accepts the function with the duration as a long milliseconds and int nanoseconds.

public static Duration

Returns:

a Duration of seconds.
get
(final String
The property name.
key
,
final TemporalUnit
The unit that the duration is measured in, not null.
unit
,
final long
The default value in the given unit.
def
)

Gets a Duration of unit stored in the system property at the given key.

private static long
getLong(final String key, final long def)

public static Duration

Returns:

a Duration of milliseconds.
getMillis
(final String
The property name.
key
,
final long
The default value in milliseconds.
def
)

Gets a Duration of milliseconds stored in the system property at the given key.

public static int

Returns:

nanoseconds between 0 and 999,999.
getNanosOfMiili
(final Duration
The duration to query.
duration
)
Deprecated Use getNanosOfMilli(Duration).

Gets the nanosecond part of a Duration converted to milliseconds.

public static int

Returns:

nanoseconds between 0 and 999,999.
getNanosOfMilli
(final Duration
The duration to query.
duration
)

Gets the nanosecond part of a Duration converted to milliseconds.

public static Duration

Returns:

a Duration of seconds.
getSeconds
(final String
The property name.
key
,
final long
The default value in seconds.
def
)

Gets a Duration of seconds stored in the system property at the given key.

public static boolean

Returns:

whether the given Duration is positive (duration > 0).
isPositive
(final Duration
the value to test
duration
)

Tests whether the given Duration is positive (duration > 0).

private static <E extends Throwable> Instant
now(final FailableConsumer<Instant, E> nowConsumer)

public static <
The type of exception throw by the lambda.
E extends Throwable
>
Duration

Returns:

The Duration of execution.
of
(final FailableConsumer<Instant, E>
What to execute.
consumer
)

Runs the lambda and returns the duration of its execution.

public static <
The type of exception throw by the lambda.
E extends Throwable
>
Duration

Returns:

The Duration of execution.
of
(final FailableRunnable<E>
What to execute.
runnable
)

Runs the lambda and returns the duration of its execution.

public static Duration

Returns:

a Duration, not null.
since
(final Temporal
the start instant, inclusive, not null.
startInclusive
)

Computes the Duration between a start instant and now.

pack-priv static ChronoUnit

Returns:

The corresponding ChronoUnit.
toChronoUnit
(final TimeUnit
A non-null TimeUnit.
timeUnit
)

Converts a TimeUnit to a ChronoUnit.

public static Duration

Returns:

a Duration.
toDuration
(final long
the amount of the duration, measured in terms of the unit, positive or negative
amount
,
final TimeUnit
the unit that the duration is measured in, must have an exact duration, not null
timeUnit
)

Converts an amount and TimeUnit into a Duration.

public static int

Returns:

int milliseconds.
toMillisInt
(final Duration
The duration to convert, not null.
duration
)

Converts a Duration to milliseconds bound to an int (instead of a long).

public static Duration

Returns:

The given duration or Duration#ZERO.
zeroIfNull
(final Duration
The duration to test.
duration
)

Returns the given non-null value or Duration#ZERO if null.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LONG_TO_INT_RANGEback to summary
pack-priv static final LongRange LONG_TO_INT_RANGE

An Integer Range that accepts Longs.

Constructor Detail

DurationUtilsback to summary
public DurationUtils()

Deprecated

TODO Make private in 4.0.

Make private in 4.0.

Annotations
@Deprecated

Method Detail

acceptback to summary
public static <T extends Throwable> void accept(final FailableBiConsumer<Long, Integer, T> consumer, final Duration duration) throws FailableBiConsumer:T

Accepts the function with the duration as a long milliseconds and int nanoseconds.

Parameters
<T>
The function exception.
consumer:FailableBiConsumer<Long, Integer, T>

Accepting function.

duration:Duration

The duration to pick apart.

Annotations
@SuppressWarnings:boxing
Exceptions
FailableBiConsumer:T:
See the function signature.
See Also
StopWatch
getback to summary
public static Duration get(final String key, final TemporalUnit unit, final long def)

Gets a Duration of unit stored in the system property at the given key.

Parameters
key:String

The property name.

unit:TemporalUnit

The unit that the duration is measured in, not null.

def:long

The default value in the given unit.

Returns:Duration

a Duration of seconds.

Since
3.19.0
getLongback to summary
private static long getLong(final String key, final long def)
getMillisback to summary
public static Duration getMillis(final String key, final long def)

Gets a Duration of milliseconds stored in the system property at the given key.

Parameters
key:String

The property name.

def:long

The default value in milliseconds.

Returns:Duration

a Duration of milliseconds.

Since
3.19.0
getNanosOfMiiliback to summary
public static int getNanosOfMiili(final Duration duration)

Deprecated

Use getNanosOfMilli(Duration).

Gets the nanosecond part of a Duration converted to milliseconds.

Handy when calling an API that takes a long of milliseconds and an int of nanoseconds. For example, Object#wait(long, int) and Thread#sleep(long, int).

Note that is this different from Duration#getNano() because a duration are seconds and nanoseconds.

Parameters
duration:Duration

The duration to query.

Returns:int

nanoseconds between 0 and 999,999.

Annotations
@Deprecated
getNanosOfMilliback to summary
public static int getNanosOfMilli(final Duration duration)

Gets the nanosecond part of a Duration converted to milliseconds.

Handy when calling an API that takes a long of milliseconds and an int of nanoseconds. For example, Object#wait(long, int) and Thread#sleep(long, int).

Note that is this different from Duration#getNano() because a duration are seconds and nanoseconds.

Parameters
duration:Duration

The duration to query.

Returns:int

nanoseconds between 0 and 999,999.

Since
3.13.0
getSecondsback to summary
public static Duration getSeconds(final String key, final long def)

Gets a Duration of seconds stored in the system property at the given key.

Parameters
key:String

The property name.

def:long

The default value in seconds.

Returns:Duration

a Duration of seconds.

Since
3.19.0
isPositiveback to summary
public static boolean isPositive(final Duration duration)

Tests whether the given Duration is positive (duration > 0).

Parameters
duration:Duration

the value to test

Returns:boolean

whether the given Duration is positive (duration > 0).

nowback to summary
private static <E extends Throwable> Instant now(final FailableConsumer<Instant, E> nowConsumer) throws FailableBiConsumer:E
ofback to summary
public static <E extends Throwable> Duration of(final FailableConsumer<Instant, E> consumer) throws FailableBiConsumer:E

Runs the lambda and returns the duration of its execution.

Parameters
<E>
The type of exception throw by the lambda.
consumer:FailableConsumer<Instant, E>

What to execute.

Returns:Duration

The Duration of execution.

Exceptions
FailableBiConsumer:E:
thrown by the lambda.
Since
3.13.0
See Also
StopWatch
ofback to summary
public static <E extends Throwable> Duration of(final FailableRunnable<E> runnable) throws FailableBiConsumer:E

Runs the lambda and returns the duration of its execution.

Parameters
<E>
The type of exception throw by the lambda.
runnable:FailableRunnable<E>

What to execute.

Returns:Duration

The Duration of execution.

Exceptions
FailableBiConsumer:E:
thrown by the lambda.
Since
3.13.0
See Also
StopWatch
sinceback to summary
public static Duration since(final Temporal startInclusive)

Computes the Duration between a start instant and now.

Parameters
startInclusive:Temporal

the start instant, inclusive, not null.

Returns:Duration

a Duration, not null.

Since
3.13.0
toChronoUnitback to summary
pack-priv static ChronoUnit toChronoUnit(final TimeUnit timeUnit)

Converts a TimeUnit to a ChronoUnit.

Parameters
timeUnit:TimeUnit

A non-null TimeUnit.

Returns:ChronoUnit

The corresponding ChronoUnit.

toDurationback to summary
public static Duration toDuration(final long amount, final TimeUnit timeUnit)

Converts an amount and TimeUnit into a Duration.

Parameters
amount:long

the amount of the duration, measured in terms of the unit, positive or negative

timeUnit:TimeUnit

the unit that the duration is measured in, must have an exact duration, not null

Returns:Duration

a Duration.

toMillisIntback to summary
public static int toMillisInt(final Duration duration)

Converts a Duration to milliseconds bound to an int (instead of a long).

Handy for low-level APIs that take millisecond timeouts in ints rather than longs.

Parameters
duration:Duration

The duration to convert, not null.

Returns:int

int milliseconds.

zeroIfNullback to summary
public static Duration zeroIfNull(final Duration duration)

Returns the given non-null value or Duration#ZERO if null.

Parameters
duration:Duration

The duration to test.

Returns:Duration

The given duration or Duration#ZERO.