Class StopWatch
StopWatch provides a convenient API for timings.
 
 To start the watch, call start() or createStarted(). At this point you can:
 
- split()the watch to get the time whilst the watch continues in the background.- unsplit()will remove the effect of the split. At this point, these three options are available again.
- suspend()the watch to pause it.- resume()allows the watch to continue. Any time between the suspend and resume will not be counted in the total. At this point, these three options are available again.
- stop()the watch to complete the timing session.
 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.
- split(),- suspend(), or- stop()cannot be invoked twice
- unsplit()may only be called if the watch has been- split()
- resume()may only be called if the watch has been- suspend()
- start()cannot be called twice without calling- reset()
This class is not thread-safe
- Since:
- 2.0
- See Also:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic StopWatchcreate()Creates a StopWatch.static StopWatchCreates and starts a StopWatch.Formats the split time withDurationFormatUtils.formatDurationHMS(long).Formats the time formatted withDurationFormatUtils.formatDurationHMS(long).Gets the Duration on the StopWatch.Gets the message for string presentation.longGets the elapsed time in nanoseconds.Gets the split Duration on the StopWatch.longGets the split time in nanoseconds.longDeprecated.Gets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.longDeprecated.UsegetStartInstant().Gets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC.longDeprecated.UsegetStopInstant().longgetTime()Deprecated.UsegetDuration().longGets the time in the specified TimeUnit.booleanTests whether the StopWatch is started.booleanTests whether StopWatch is stopped.booleanTests whether the StopWatch is suspended.voidreset()Resets the StopWatch.voidresume()Resumes the StopWatch after a suspend.voidsplit()Splits the time.voidstart()Starts the StopWatch.voidstop()Stops the StopWatch.voidsuspend()Suspends the StopWatch for later resumption.Gets a summary of the split time that the StopWatch recorded as a string.toString()Gets a summary of the time that the StopWatch recorded as a string.voidunsplit()Removes a split.
- 
Constructor Details- 
StopWatchpublic StopWatch()Constructs a new instance.
- 
StopWatchConstructs a new instance.- Parameters:
- message- A message for string presentation.
- Since:
- 3.10
 
 
- 
- 
Method Details- 
createCreates a StopWatch.- Returns:
- StopWatch a StopWatch.
- Since:
- 3.10
 
- 
createStartedCreates and starts a StopWatch.- Returns:
- StopWatch a started StopWatch.
- Since:
- 3.5
 
- 
formatSplitTimeFormats the split time withDurationFormatUtils.formatDurationHMS(long).- Returns:
- the split time formatted by DurationFormatUtils.formatDurationHMS(long).
- Since:
- 3.10
 
- 
formatTimeFormats the time formatted withDurationFormatUtils.formatDurationHMS(long).- Returns:
- the time formatted by DurationFormatUtils.formatDurationHMS(long).
- Since:
- 3.10
 
- 
getDurationGets the Duration on the StopWatch.This is either the Duration between the start and the moment this method is called, or the Duration between start and stop. - Returns:
- the Duration.
- Since:
- 3.16.0
 
- 
getMessageGets the message for string presentation.- Returns:
- the message for string presentation.
- Since:
- 3.10
 
- 
getNanoTimeGets 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:
- the elapsed time in nanoseconds.
- Since:
- 3.0
- See Also:
 
- 
getSplitDurationGets the split Duration on the StopWatch.This is the Duration between start and latest split. - Returns:
- the split Duration
- Throws:
- IllegalStateException- if the StopWatch has not yet been split.
- Since:
- 3.16.0
 
- 
getSplitNanoTimeGets the split time in nanoseconds.This is the time between start and latest split. - Returns:
- the split time in nanoseconds
- Throws:
- IllegalStateException- if the StopWatch has not yet been split.
- Since:
- 3.0
 
- 
getSplitTimeDeprecated.UsegetSplitDuration().Gets the split time on the StopWatch.This is the time between start and latest split. - Returns:
- the split time in milliseconds
- Throws:
- IllegalStateException- if the StopWatch has not yet been split.
- Since:
- 2.1
 
- 
getStartInstantGets the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.- Returns:
- the Instant this StopWatch was started, between the current time and midnight, January 1, 1970 UTC.
- Throws:
- IllegalStateException- if this StopWatch has not been started
- Since:
- 3.16.0
 
- 
getStartTimeDeprecated.UsegetStartInstant().Gets the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.- Returns:
- the time this StopWatch was started in milliseconds, between the current time and midnight, January 1, 1970 UTC.
- Throws:
- IllegalStateException- if this StopWatch has not been started
- Since:
- 2.4
 
- 
getStopInstantGets the Instant this StopWatch was stopped, between the current time and midnight, January 1, 1970 UTC.- Returns:
- the Instant this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
- Throws:
- IllegalStateException- if this StopWatch has not been started
- Since:
- 3.16.0
 
- 
getStopTimeDeprecated.UsegetStopInstant().Gets the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.- Returns:
- the time this StopWatch was stopped in milliseconds, between the current time and midnight, January 1, 1970 UTC.
- Throws:
- IllegalStateException- if this StopWatch has not been started
- Since:
- 3.12.0
 
- 
getTimeDeprecated.UsegetDuration().Gets the time on the 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:
- the time in milliseconds
 
- 
getTimeGets 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.HOURSand the StopWatch time is 59 minutes, then the result returned will be0.- Parameters:
- timeUnit- the unit of time, not null
- Returns:
- the time in the specified TimeUnit, rounded down
- Since:
- 3.5
 
- 
isStartedTests whether the StopWatch is started. A suspended StopWatch is also started watch.- Returns:
- boolean If the StopWatch is started.
- Since:
- 3.2
 
- 
isStoppedTests whether StopWatch is stopped. The StopWatch which's not yet started and explicitly stopped StopWatch is considered as stopped.- Returns:
- boolean If the StopWatch is stopped.
- Since:
- 3.2
 
- 
isSuspendedTests whether the StopWatch is suspended.- Returns:
- boolean If the StopWatch is suspended.
- Since:
- 3.2
 
- 
resetResets the StopWatch. Stops it if need be.This method clears the internal values to allow the object to be reused. 
- 
resumeResumes the 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. - Throws:
- IllegalStateException- if the StopWatch has not been suspended.
 
- 
splitSplits 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.- Throws:
- IllegalStateException- if the StopWatch is not running.
 
- 
startStarts the StopWatch.This method starts a new timing session, clearing any previous values. - Throws:
- IllegalStateException- if the StopWatch is already running.
 
- 
stopStops the StopWatch.This method ends a new timing session, allowing the time to be retrieved. - Throws:
- IllegalStateException- if the StopWatch is not running.
 
- 
suspendSuspends the 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. - Throws:
- IllegalStateException- if the StopWatch is not currently running.
 
- 
toSplitStringGets a summary of the split time that the StopWatch recorded as a string.The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds. - Returns:
- the split time as a String
- Since:
- 2.1, 3.10 Returns the prefix "message "if the message is set.
 
- 
toStringGets a summary of the time that the StopWatch recorded as a string.The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds. 
- 
unsplitRemoves a split.This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue. - Throws:
- IllegalStateException- if the StopWatch has not been split.
 
 
- 
getSplitDuration().