public class SetTimeProvider extends AtomicLong implements TimeProvider
TimeProvider implementation that allows explicit setting and manipulation of time values
for testing purposes. This class supports time values in milliseconds, microseconds, and nanoseconds.
It can be set to a specific time and can be auto-incremented at each call, which is useful for
simulating time progression in a controlled testing environment.| Constructor and Description |
|---|
SetTimeProvider()
Constructs a time provider initialised to zero nanoseconds.
|
SetTimeProvider(Instant instant)
Constructs a time provider starting at a given
Instant. |
SetTimeProvider(long initialNanos)
Constructs a time provider starting at a specific time in nanoseconds.
|
SetTimeProvider(String timestamp)
Constructs a time provider starting at a time specified in ISO8601 format.
|
| Modifier and Type | Method and Description |
|---|---|
SetTimeProvider |
advanceMicros(long micros)
Advances the current time by the specified duration in microseconds.
|
SetTimeProvider |
advanceMillis(long millis)
Advances the current time by the specified duration in milliseconds.
|
SetTimeProvider |
advanceNanos(long nanos)
Advances the current time by the specified duration in nanoseconds.
|
SetTimeProvider |
autoIncrement(long autoIncrement,
TimeUnit timeUnit)
Enables time auto-increment after each call to
currentTimeNanos(). |
long |
currentTime(TimeUnit unit)
Converts and retrieves the current time in the specified unit.
|
long |
currentTimeMicros()
Retrieves the current time in microseconds.
|
void |
currentTimeMicros(long micros)
Explicitly sets the current time in microseconds.
|
long |
currentTimeMillis()
Retrieves the current time in milliseconds.
|
void |
currentTimeMillis(long millis)
Explicitly sets the current time in milliseconds.
|
long |
currentTimeNanos()
Retrieves the current time in nanoseconds.
|
void |
currentTimeNanos(long nanos)
Explicitly sets the current time in nanoseconds.
|
SetTimeProvider |
now()
Constructs a time provider that starts now.
|
String |
toString()
Provides a string representation of the provider state.
|
accumulateAndGet, addAndGet, compareAndSet, decrementAndGet, doubleValue, floatValue, get, getAndAccumulate, getAndAdd, getAndDecrement, getAndIncrement, getAndSet, getAndUpdate, incrementAndGet, intValue, lazySet, longValue, set, updateAndGet, weakCompareAndSetbyteValue, shortValuepublic SetTimeProvider()
public SetTimeProvider(long initialNanos)
initialNanos - starting time in nanoseconds, non-negativepublic SetTimeProvider(String timestamp)
timestamp - ISO8601 timestamp, not nullpublic SetTimeProvider now()
public SetTimeProvider autoIncrement(long autoIncrement, TimeUnit timeUnit)
currentTimeNanos().
Example usage in a JUnit test:
SetTimeProvider tp = new SetTimeProvider("2023-01-01T00:00:00")
.autoIncrement(1, TimeUnit.MILLISECONDS);
Every read of the current time now advances by one millisecond. When running tests concurrently, coordinate access to avoid unexpected jumps.
autoIncrement - increment amount, non-negativetimeUnit - unit of autoIncrementpublic void currentTimeMillis(long millis)
throws IllegalArgumentException
millis - new time in milliseconds, not less than the current valueIllegalArgumentException - if the time would go backwardspublic long currentTimeMillis()
currentTimeMillis in interface TimeProviderpublic void currentTimeMicros(long micros)
throws IllegalArgumentException
micros - new time in microseconds, not less than the current valueIllegalArgumentException - if the time would go backwardspublic long currentTimeMicros()
currentTimeMicros in interface TimeProviderpublic void currentTimeNanos(long nanos)
throws IllegalArgumentException
nanos - new time in nanoseconds, not less than the current valueIllegalArgumentException - if the time would go backwardspublic long currentTimeNanos()
currentTimeNanos in interface TimeProviderpublic long currentTime(TimeUnit unit)
unit - target unit, not nullpublic SetTimeProvider advanceMillis(long millis)
millis - duration to add, may be negativepublic SetTimeProvider advanceMicros(long micros)
micros - duration to add, may be negativepublic SetTimeProvider advanceNanos(long nanos)
nanos - duration to add, may be negativepublic String toString()
toString in class AtomicLongCopyright © 2026 Chronicle Software Ltd. All rights reserved.