public class Clock
extends java.lang.Object
System.currentTimeMillis()
calls (which perform poorly because they require system
calls).
Each call to lastKnownTimeMillis()
will return the value of
System.currentTimeMillis()
as of the last call to accurateTimeMillis()
.
This means lastKnownTimeMillis()
will only be as accurate as the frequency with which
accurateTimeMillis()
is called.
In order to ensure a minimum level of accuracy, by default a thread is started to call
accurateTimeMillis()
every 100 milliseconds. This can be disabled by calling
stopClockUpdateThread()
.
Modifier and Type | Field and Description |
---|---|
static short |
AUTOMATIC_UPDATE_FREQUENCY_IN_MS
This is the frequency at which the thread which regularly updates the clock wakes up and
updates the time.
|
static int |
NANOS_IN_MILLISECOND
Simple conversion of how many nanoseconds exist within a single millisecond.
|
Constructor and Description |
---|
Clock() |
Modifier and Type | Method and Description |
---|---|
static long |
accurateForwardProgressingMillis()
Returns an accurate amount of time in milliseconds since this class has loaded (starting at
0 ). |
static long |
accurateTimeMillis()
Updates the clock so that future calls to
lastKnownTimeMillis() can benefit, and
returns the accurate time in milliseconds. |
static long |
accurateTimeNanos()
This directly returns the result of
System.nanoTime() . |
static long |
forwardProgressingDuration(long forwardMillis)
Finds the duration in milliseconds from the reference time.
|
static long |
lastKnownForwardProgressingMillis()
Returns a fuzzy time for how much time in milliseconds since this class has loaded (starting
at
0 ). |
static long |
lastKnownTimeMillis()
Getter for the last known time in milliseconds.
|
static long |
lastKnownTimeNanos()
This returns a fuzzy known nano time from invocations to either
accurateTimeNanos()
or accurateForwardProgressingMillis() . |
static void |
startClockUpdateThread()
Starts a thread to regularly updated the clock automatically.
|
static void |
stopClockUpdateThread()
Stops the clock from updating automatically.
|
public static final int NANOS_IN_MILLISECOND
public static final short AUTOMATIC_UPDATE_FREQUENCY_IN_MS
accurateForwardProgressingMillis()
and
accurateTimeMillis()
both update their respective times, but this will make sure
that even if those calls are not made requests to lastKnownForwardProgressingMillis()
and lastKnownTimeMillis()
have at least some level of accuracy.public static void startClockUpdateThread()
public static void stopClockUpdateThread()
public static long accurateTimeNanos()
System.nanoTime()
. Using this as an alternative
to invoking System.nanoTime()
directly is that it updates the nano time
representation, allowing for more accurate time references when calling
lastKnownTimeNanos()
and lastKnownForwardProgressingMillis()
.
Please read the java documentation about System.nanoTime()
to understand the nature
of this value (it may be positive, negative, overflow, and is completely arbitrary from its
start point).
public static long lastKnownTimeNanos()
accurateTimeNanos()
or accurateForwardProgressingMillis()
. In addition (unless manually stopped via
stopClockUpdateThread()
) this time is updated at the frequency of
AUTOMATIC_UPDATE_FREQUENCY_IN_MS
. Thus providing a minimal level of accuracy.
Please read the java documentation about System.nanoTime()
to understand the nature
of this value (it may be positive, negative, overflow, and is completely arbitrary from its
start point).
public static long lastKnownForwardProgressingMillis()
0
). If Clock
was loaded at the start of the application, this can provide
the amount of time the application has been running.
This call is guaranteed to only progress forward, regardless of system clock changes it will move forward at a consistent rate.
By default (unless manually stopped via stopClockUpdateThread()
) this time is
updated automatically at the frequency of AUTOMATIC_UPDATE_FREQUENCY_IN_MS
. Thus
allowing a guarantee of minimal accuracy within the set milliseconds.
public static long accurateForwardProgressingMillis()
0
). If Clock
was loaded at the start of the application, this can provide
the amount of time the application has been running. Calls to this will NOT update the time
in accurateTimeMillis()
.
This call is guaranteed to only progress forward, regardless of system clock changes it will move forward at a consistent rate.
public static long forwardProgressingDuration(long forwardMillis)
lastKnownForwardProgressingMillis()
from the parameter
provided. The parameter provided should have been a result from
lastKnownForwardProgressingMillis()
or accurateForwardProgressingMillis()
.
Manipulating that time before calling this is not supported (negative results will not be
provided). This is simply a helper function for this very common operation.forwardMillis
- The reference timepublic static long lastKnownTimeMillis()
If the system clock goes backwards this too can go backwards. If that is not desirable
consider using lastKnownForwardProgressingMillis()
.
By default (unless manually stopped via stopClockUpdateThread()
) this time is
updated automatically at the frequency of AUTOMATIC_UPDATE_FREQUENCY_IN_MS
. Thus
allowing a guarantee of minimal accuracy within the set milliseconds.
public static long accurateTimeMillis()
lastKnownTimeMillis()
can benefit, and
returns the accurate time in milliseconds. This will NOT update the time for calls to
lastKnownForwardProgressingMillis()
.
If the system clock goes backwards this too can go backwards. If that is not desirable
consider using accurateForwardProgressingMillis()
.