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

public Class StopWatch

extends Object
Class Inheritance
Imports
java.time.Duration, .Instant, java.util.ArrayList, .Collections, .List, .Objects, java.util.concurrent.TimeUnit, java.util.function.Supplier, org.apache.commons.lang3.StringUtils, org.apache.commons.lang3.function.FailableConsumer, .FailableRunnable, .FailableSupplier, org.apache.commons.lang3.tuple.ImmutablePair

StopWatch provides a convenient API for timings.

To start the watch, call start() or StopWatch#createStarted(). At this point you can:

It is intended that the output methods toString() and getTime() should only be called after stop, split or suspend, however a suitable result will be returned at other points.

Note

As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start, resume before suspend or unsplit before split.

  1. split(), suspend(), or stop() cannot be invoked twice
  2. unsplit() may only be called if the watch has been split()
  3. resume() may only be called if the watch has been suspend()
  4. start() cannot be called twice without calling reset()

This class is not thread-safe.

Since
2.0
See Also
DurationUtils#of(FailableRunnable), DurationUtils#of(FailableConsumer)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
StopWatch.Split

Stores a split as a label and duration.

private static enum
StopWatch.SplitState

Enumeration type which indicates the split status of a StopWatch.

private static enum
StopWatch.State

Enumeration type which indicates the status of a StopWatch.

Field Summary

Modifier and TypeField and Description
private final String
message

A message for string presentation.

private static final long
private StopWatch.State
runningState

The current running state of this StopWatch.

private final List<StopWatch.Split>
splits

The split list.

private StopWatch.SplitState
splitState

Whether this StopWatch has a split time recorded.

private Instant
startInstant

The start Instant.

private long
startTimeNanos

The start time in nanoseconds.

private Instant
stopInstant

The end Instant.

private long
stopTimeNanos

The stop time in nanoseconds.

Constructor Summary

AccessConstructor and Description
public
StopWatch()

Constructs a new instance.

public
StopWatch(final String
A message for string presentation.
message
)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public static StopWatch

Returns:

StopWatch a StopWatch.
create
()

Creates a StopWatch.

public static StopWatch

Returns:

StopWatch a started StopWatch.
createStarted
()

Creates and starts a StopWatch.

public String

Returns:

the split time formatted by DurationFormatUtils#formatDurationHMS.
formatSplitTime
()

Formats the split time with DurationFormatUtils#formatDurationHMS.

public String

Returns:

the time formatted by DurationFormatUtils#formatDurationHMS.
formatTime
()

Formats the time formatted with DurationFormatUtils#formatDurationHMS.

public <
the type of results supplied by this supplier.
T
>
T

Returns:

a result from the given Supplier.
get
(final Supplier<T>
The supplier to Supplier#get().
supplier
)

Delegates to Supplier#get() while recording the duration of the call.

public Duration

Returns:

the Duration.
getDuration
()

Gets the Duration on this StopWatch.

public String

Returns:

the message for string presentation.
getMessage
()

Gets the message for string presentation.

public long

Returns:

the elapsed time in nanoseconds.
getNanoTime
()

Gets the elapsed time in nanoseconds.

public Duration

Returns:

the split Duration.
getSplitDuration
()

Gets the split Duration on this StopWatch.

public long

Returns:

the split time in nanoseconds.
getSplitNanoTime
()

Gets the split time in nanoseconds.

public List<StopWatch.Split>

Returns:

the list of splits.
getSplits
()

Gets the split list.

public long

Returns:

the split time in milliseconds.
getSplitTime
()
Deprecated Use getSplitDuration().

Gets the split time on this StopWatch.

public Instant

Returns:

the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.
getStartInstant
()

Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.

public long

Returns:

the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
getStartTime
()
Deprecated Use getStartInstant().

Gets the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.

public Instant

Returns:

the Instant this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
getStopInstant
()

Gets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC.

public long

Returns:

the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
getStopTime
()
Deprecated Use getStopInstant().

Gets the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.

public <
the type of results supplied by this supplier.
T
,
The kind of thrown exception or error.
E extends Throwable
>
T

Returns:

a result from the given Supplier.
getT
(final FailableSupplier<T, E>
The supplier to Supplier#get().
supplier
)

Delegates to FailableSupplier#get() while recording the duration of the call.

public long

Returns:

the time in milliseconds.
getTime
()

Gets the time on this StopWatch.

public long

Returns:

the time in the specified TimeUnit, rounded down.
getTime
(final TimeUnit
the unit of time, not null.
timeUnit
)

Gets the time in the specified TimeUnit.

public boolean

Returns:

boolean If this StopWatch is started.
isStarted
()

Tests whether this StopWatch is started.

public boolean

Returns:

boolean If this StopWatch is stopped.
isStopped
()

Tests whether StopWatch is stopped.

public boolean

Returns:

boolean If this StopWatch is suspended.
isSuspended
()

Tests whether this StopWatch is suspended.

private long

Returns:

milliseconds conversion result.
nanosToMillis
(final long
nanoseconds to convert.
nanos
)

Converts nanoseconds to milliseconds.

public void
reset()

Resets this StopWatch.

public void
resume()

Resumes this StopWatch after a suspend.

public void
run(final Runnable
The runnable to Runnable#run().
runnable
)

Delegates to Runnable#run() while recording the duration of the call.

public <
The kind of Throwable.
E extends Throwable
>
void
runT(final FailableRunnable<E>
The runnable to FailableRunnable#run().
runnable
)

Delegates to FailableRunnable#run() while recording the duration of the call.

public void
split()

Splits the time.

public void
split(final String
A message for string presentation.
label
)

Splits the time with a label.

public void
start()

Starts this StopWatch.

private void
startResume()

Starts or resumes this StopWatch.

public void
stop()

Stops this StopWatch.

public void
suspend()

Suspends this StopWatch for later resumption.

public String

Returns:

the split time as a String.
toSplitString
()

Gets a summary of the last split time that this StopWatch recorded as a string.

public String

Returns:

the time as a String.
toString
()

Overrides java.lang.Object.toString.

Gets a summary of the time that this StopWatch recorded as a string.

public void
unsplit()

Removes the split.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

messageback to summary
private final String message

A message for string presentation.

Since
3.10
NANO_2_MILLISback to summary
private static final long NANO_2_MILLIS
runningStateback to summary
private StopWatch.State runningState

The current running state of this StopWatch.

splitsback to summary
private final List<StopWatch.Split> splits

The split list.

splitStateback to summary
private StopWatch.SplitState splitState

Whether this StopWatch has a split time recorded.

startInstantback to summary
private Instant startInstant

The start Instant.

nanoTime is only for elapsed time so we need to also store the currentTimeMillis to maintain the old getStartTime API.

On Java 8, Instant has millisecond precision, later versions use nanoseconds.

startTimeNanosback to summary
private long startTimeNanos

The start time in nanoseconds. This field can be removed once we move off of Java 8.

stopInstantback to summary
private Instant stopInstant

The end Instant.

nanoTime is only for elapsed time so we need to also store the currentTimeMillis to maintain the old getStartTime API.

On Java 8, Instant has millisecond precision, later versions use nanoseconds.

stopTimeNanosback to summary
private long stopTimeNanos

The stop time in nanoseconds. This field can be removed once we move off of Java 8.

Constructor Detail

StopWatchback to summary
public StopWatch()

Constructs a new instance.

StopWatchback to summary
public StopWatch(final String message)

Constructs a new instance.

Parameters
message:String

A message for string presentation.

Since
3.10

Method Detail

createback to summary
public static StopWatch create()

Creates a StopWatch.

Returns:StopWatch

StopWatch a StopWatch.

Since
3.10
createStartedback to summary
public static StopWatch createStarted()

Creates and starts a StopWatch.

Returns:StopWatch

StopWatch a started StopWatch.

Since
3.5
formatSplitTimeback to summary
public String formatSplitTime()

Formats the split time with DurationFormatUtils#formatDurationHMS.

Returns:String

the split time formatted by DurationFormatUtils#formatDurationHMS.

Since
3.10
formatTimeback to summary
public String formatTime()

Formats the time formatted with DurationFormatUtils#formatDurationHMS.

Returns:String

the time formatted by DurationFormatUtils#formatDurationHMS.

Since
3.10
getback to summary
public <T> T get(final Supplier<T> supplier)

Delegates to Supplier#get() while recording the duration of the call.

Parameters
<T>
the type of results supplied by this supplier.
supplier:Supplier<T>

The supplier to Supplier#get().

Returns:T

a result from the given Supplier.

Since
3.18.0
getDurationback to summary
public Duration getDuration()

Gets the Duration on this StopWatch.

This is either the Duration between the start and the moment this method is called, or the Duration between start and stop.

Returns:Duration

the Duration.

Since
3.16.0
getMessageback to summary
public String getMessage()

Gets the message for string presentation.

Returns:String

the message for string presentation.

Since
3.10
getNanoTimeback to summary
public long getNanoTime()

Gets the elapsed time in nanoseconds.

This is either the time between the start and the moment this method is called, or the amount of time between start and stop.

Returns:long

the elapsed time in nanoseconds.

Since
3.0
See Also
System#nanoTime()
getSplitDurationback to summary
public Duration getSplitDuration()

Gets the split Duration on this StopWatch.

This is the Duration between start and latest split.

Returns:Duration

the split Duration.

Exceptions
IllegalStateException:
if this StopWatch has not yet been split.
Since
3.16.0
getSplitNanoTimeback to summary
public long getSplitNanoTime()

Gets the split time in nanoseconds.

This is the time between start and latest split.

Returns:long

the split time in nanoseconds.

Exceptions
IllegalStateException:
if this StopWatch has not yet been split.
Since
3.0
getSplitsback to summary
public List<StopWatch.Split> getSplits()

Gets the split list.

Returns:List<StopWatch.Split>

the list of splits.

Since
3.20.0
getSplitTimeback to summary
public long getSplitTime()

Deprecated

Use getSplitDuration().

Gets the split time on this StopWatch.

This is the time between start and latest split.

Returns:long

the split time in milliseconds.

Annotations
@Deprecated
Exceptions
IllegalStateException:
if this StopWatch has not yet been split.
Since
2.1
getStartInstantback to summary
public Instant getStartInstant()

Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.

Returns:Instant

the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.

Exceptions
IllegalStateException:
if this StopWatch has not been started.
Since
3.16.0
getStartTimeback to summary
public long getStartTime()

Deprecated

Use getStartInstant().

Gets the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Returns:long

the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Annotations
@Deprecated
Exceptions
IllegalStateException:
if this StopWatch has not been started.
Since
2.4
getStopInstantback to summary
public Instant getStopInstant()

Gets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC.

Returns:Instant

the Instant this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Exceptions
IllegalStateException:
if this StopWatch has not been started.
Since
3.16.0
getStopTimeback to summary
public long getStopTime()

Deprecated

Use getStopInstant().

Gets the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Returns:long

the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Annotations
@Deprecated
Exceptions
IllegalStateException:
if this StopWatch has not been started.
Since
3.12.0
getTback to summary
public <T, E extends Throwable> T getT(final FailableSupplier<T, E> supplier) throws Throwable

Delegates to FailableSupplier#get() while recording the duration of the call.

Parameters
<T>
the type of results supplied by this supplier.
<E>
The kind of thrown exception or error.
supplier:FailableSupplier<T, E>

The supplier to Supplier#get().

Returns:T

a result from the given Supplier.

Exceptions
Throwable:
if the supplier fails.
Since
3.18.0
getTimeback to summary
public long getTime()

Gets the time on this StopWatch.

This is either the time between the start and the moment this method is called, or the amount of time between start and stop.

Returns:long

the time in milliseconds.

See Also
getDuration()
getTimeback to summary
public long getTime(final TimeUnit timeUnit)

Gets the time in the specified TimeUnit.

This is either the time between the start and the moment this method is called, or the amount of time between start and stop. The resulting time will be expressed in the desired TimeUnit with any remainder rounded down. For example, if the specified unit is TimeUnit.HOURS and this StopWatch time is 59 minutes, then the result returned will be 0.

Parameters
timeUnit:TimeUnit

the unit of time, not null.

Returns:long

the time in the specified TimeUnit, rounded down.

Since
3.5
isStartedback to summary
public boolean isStarted()

Tests whether this StopWatch is started. A suspended StopWatch is also started watch.

Returns:boolean

boolean If this StopWatch is started.

Since
3.2
isStoppedback to summary
public boolean isStopped()

Tests whether StopWatch is stopped. this StopWatch which's not yet started and explicitly stopped StopWatch is considered as stopped.

Returns:boolean

boolean If this StopWatch is stopped.

Since
3.2
isSuspendedback to summary
public boolean isSuspended()

Tests whether this StopWatch is suspended.

Returns:boolean

boolean If this StopWatch is suspended.

Since
3.2
nanosToMillisback to summary
private long nanosToMillis(final long nanos)

Converts nanoseconds to milliseconds.

Parameters
nanos:long

nanoseconds to convert.

Returns:long

milliseconds conversion result.

resetback to summary
public void reset()

Resets this StopWatch. Stops it if need be.

This method clears the internal values to allow the object to be reused.

resumeback to summary
public void resume()

Resumes this StopWatch after a suspend.

This method resumes the watch after it was suspended. The watch will not include time between the suspend and resume calls in the total time.

Exceptions
IllegalStateException:
if this StopWatch has not been suspended.
runback to summary
public void run(final Runnable runnable)

Delegates to Runnable#run() while recording the duration of the call.

Parameters
runnable:Runnable

The runnable to Runnable#run().

Since
3.18.0
runTback to summary
public <E extends Throwable> void runT(final FailableRunnable<E> runnable) throws Throwable

Delegates to FailableRunnable#run() while recording the duration of the call.

Parameters
<E>
The kind of Throwable.
runnable:FailableRunnable<E>

The runnable to FailableRunnable#run().

Exceptions
Throwable:
Thrown by FailableRunnable#run().
Since
3.18.0
splitback to summary
public void split()

Splits the time.

This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected, enabling unsplit() to continue the timing from the original start point.

Exceptions
IllegalStateException:
if this StopWatch is not running.
splitback to summary
public void split(final String label)

Splits the time with a label.

This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected, enabling unsplit() to continue the timing from the original start point.

Parameters
label:String

A message for string presentation.

Exceptions
IllegalStateException:
if the StopWatch is not running.
Since
3.20.0
startback to summary
public void start()

Starts this StopWatch.

This method starts a new timing session, clearing any previous values.

Exceptions
IllegalStateException:
if this StopWatch is already running.
startResumeback to summary
private void startResume()

Starts or resumes this StopWatch.

stopback to summary
public void stop()

Stops this StopWatch.

This method ends a new timing session, allowing the time to be retrieved.

Exceptions
IllegalStateException:
if this StopWatch is not running.
suspendback to summary
public void suspend()

Suspends this StopWatch for later resumption.

This method suspends the watch until it is resumed. The watch will not include time between the suspend and resume calls in the total time.

Exceptions
IllegalStateException:
if this StopWatch is not currently running.
toSplitStringback to summary
public String toSplitString()

Gets a summary of the last split time that this StopWatch recorded as a string.

The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds.

Returns:String

the split time as a String.

Since
2.1, 3.10 Returns the prefix "message " if the message is set.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Gets a summary of the time that this StopWatch recorded as a string.

The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds.

Returns:String

the time as a String.

Annotations
@Override
Since
3.10 Returns the prefix "message " if the message is set.
unsplitback to summary
public void unsplit()

Removes the split.

This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue.

Exceptions
IllegalStateException:
if this StopWatch has not been split.
org.apache.commons.lang3.time back to summary

public final Class StopWatch.Split

extends ImmutablePair<String, Duration>
Class Inheritance

Stores a split as a label and duration.
Since
3.20.0

Field Summary

Inherited from org.apache.commons.lang3.tuple.ImmutablePair:
EMPTY_ARRAYleftright

Constructor Summary

AccessConstructor and Description
public
Split(String
Label for this split.
label
,
Duration
Duration for this split.
duration
)

Constructs a Split object with label and duration.

Method Summary

Modifier and TypeMethod and Description
public Duration

Returns:

The duration of this split..
getDuration
()

Gets the duration of this split.

public String

Returns:

The label of this split.
getLabel
()

Gets the label of this split.

public String

Returns:

this instance to a string.
toString
()

Overrides org.apache.commons.lang3.tuple.Pair.toString.

Converts this instance to a string.

Inherited from org.apache.commons.lang3.tuple.ImmutablePair:
emptyArraygetLeftgetRightleftnullPairofofofNonNullrightsetValue

Constructor Detail

Splitback to summary
public Split(String label, Duration duration)

Constructs a Split object with label and duration.

Parameters
label:String

Label for this split.

duration:Duration

Duration for this split.

Method Detail

getDurationback to summary
public Duration getDuration()

Gets the duration of this split.

Returns:Duration

The duration of this split..

getLabelback to summary
public String getLabel()

Gets the label of this split.

Returns:String

The label of this split.

toStringback to summary
public String toString()

Overrides org.apache.commons.lang3.tuple.Pair.toString.

Converts this instance to a string.

Returns:String

this instance to a string.

Annotations
@Override
org.apache.commons.lang3.time back to summary

private final Enum StopWatch.SplitState

extends Enum<StopWatch.SplitState>
Class Inheritance

Enumeration type which indicates the split status of a StopWatch.

Field Summary

Modifier and TypeField and Description
public static final StopWatch.SplitState
public static final StopWatch.SplitState

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static StopWatch.SplitState
public static StopWatch.SplitState[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

SPLITback to summary
public static final StopWatch.SplitState SPLIT
UNSPLITback to summary
public static final StopWatch.SplitState UNSPLIT

Constructor Detail

SplitStateback to summary
private SplitState()

Method Detail

valueOfback to summary
public static StopWatch.SplitState valueOf(String name)
valuesback to summary
public static StopWatch.SplitState[] values()
org.apache.commons.lang3.time back to summary

private sealed Enum StopWatch.State

extends Enum<StopWatch.State>
Class Inheritance

Enumeration type which indicates the status of a StopWatch.

Field Summary

Modifier and TypeField and Description
public static final StopWatch.State
public static final StopWatch.State
public static final StopWatch.State
public static final StopWatch.State

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract boolean

Returns:

boolean If this StopWatch is started.
isStarted
()

Tests whether this StopWatch is started.

pack-priv abstract boolean

Returns:

boolean If this StopWatch is stopped.
isStopped
()

Tests whether this StopWatch is stopped.

pack-priv abstract boolean

Returns:

boolean If this StopWatch is suspended.
isSuspended
()

Tests whether this StopWatch is suspended.

public static StopWatch.State
public static StopWatch.State[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

RUNNINGback to summary
public static final StopWatch.State RUNNING
STOPPEDback to summary
public static final StopWatch.State STOPPED
SUSPENDEDback to summary
public static final StopWatch.State SUSPENDED
UNSTARTEDback to summary
public static final StopWatch.State UNSTARTED

Constructor Detail

Stateback to summary
private State()

Method Detail

isStartedback to summary
pack-priv abstract boolean isStarted()

Tests whether this StopWatch is started. A suspended StopWatch is also started.

Returns:boolean

boolean If this StopWatch is started.

isStoppedback to summary
pack-priv abstract boolean isStopped()

Tests whether this StopWatch is stopped. A StopWatch which is not yet started and explicitly stopped is considered stopped.

Returns:boolean

boolean If this StopWatch is stopped.

isSuspendedback to summary
pack-priv abstract boolean isSuspended()

Tests whether this StopWatch is suspended.

Returns:boolean

boolean If this StopWatch is suspended.

valueOfback to summary
public static StopWatch.State valueOf(String name)
valuesback to summary
public static StopWatch.State[] values()