public class SimpleStopWatch
extends java.lang.Object
This stopwatch uses System.nanoTime() to retrieve both the start-time and stop-time. A
regular run expects you to call start(), followed by stop(), and then either of
getTime(), getTime(String), or toString() to retrieve the elapsed time
as a formatted string.
Hours are calculated in a 24h-format: elapsed time / 1.000.000.000 / 3600 % 24
Minutes are calculated: elapsed time / 1.000.000.000 / 60 % 60
Seconds are calculated: elapsed time / 1.000.000.000 % 60
Milliseconds are calculated: elapsed time / 1.000.000
| Constructor and Description |
|---|
SimpleStopWatch() |
| Modifier and Type | Method and Description |
|---|---|
long |
getElapsedNanoseconds()
Get the elapsed time in nanoseconds.
|
@NotNull java.lang.String |
getTime()
Get the elapsed time of this stopwatch, formatted using
%2dh:%02dm:%02ds:%04dms. |
@NotNull java.lang.String |
getTime(@NotNull java.lang.String format)
Get the elapsed time of this stopwatch using a custom formatting.
|
@NotNull SimpleStopWatch |
start()
Starts the stopwatch.
|
@NotNull SimpleStopWatch |
stop()
Stops the stopwatch.
|
@NotNull java.lang.String |
toString()
Get the elapsed time of this stopwatch, formatted using
%2dh:%02dm:%02ds:%04dms. |
@NotNull public @NotNull SimpleStopWatch start()
After having started the stopwatch, call stop(), followed by any of
@NotNull public @NotNull SimpleStopWatch stop() throws java.lang.IllegalStateException
The stopwatch must have been started before calling this method, otherwise an
IllegalStateException will be thrown. Call start() first!
java.lang.IllegalStateException - if the stopwatch was stopped without having been started first.@NotNull
public @NotNull java.lang.String toString()
throws java.lang.IllegalStateException
%2dh:%02dm:%02ds:%04dms.
The stopwatch must be started and stopped before calling this method, getTime(), or
getTime(String). If any of the aforementioned methods are called, but the stopwatch
was not started and stopped yet first, an IllegalStateException will be thrown.
toString in class java.lang.Object%2d:%02d:%02d.java.lang.IllegalStateException - if the stopwatch was started, but not stopped yet.@NotNull
public @NotNull java.lang.String getTime()
throws java.lang.IllegalStateException
%2dh:%02dm:%02ds:%04dms.
The stopwatch must be started and stopped before calling this method, getTime(String),
or toString(). If any of the aforementioned methods are called, but the stopwatch was
not started and stopped yet first, an IllegalStateException will be thrown.
%2d:%02d:%02d.java.lang.IllegalStateException - if the stopwatch was started, but not stopped yet.@NotNull
public @NotNull java.lang.String getTime(@NotNull
@NotNull java.lang.String format)
throws java.lang.IllegalStateException
The stopwatch must be started and stopped before calling this method, getTime(), or
toString(). If any of the aforementioned methods are called, but the stopwatch was not
started and stopped yet first, an IllegalStateException will be thrown.
format - Formatting to apply to the elapsed hours, minutes and
seconds.java.lang.IllegalStateException - if the stopwatch was started, but not stopped yet.public long getElapsedNanoseconds()
throws java.lang.IllegalStateException
IllegalStateException may be thrown.
IllegalStateException is thrown.java.lang.IllegalStateException - when the stopwatch is in an invalid state for time-retrieval.