Class MonotonicClock
- All Implemented Interfaces:
- InstantSource
 This class provides precise time measurements using System.nanoTime()
 while maintaining wall-clock time information in UTC. The wall-clock time
 is computed from the monotonic duration since system start to ensure consistency
 between time measurements.
 
 This implementation is singleton-based and always uses UTC timezone. The clock
 cannot be adjusted to different timezones to maintain consistent monotonic behavior.
 Users needing local time representation should convert the result of instant()
 to their desired timezone:
 
 Instant now = MonotonicClock.now();
 ZonedDateTime local = now.atZone(ZoneId.systemDefault());
 - See Also:
- 
Method SummaryModifier and TypeMethodDescriptionstatic Durationelapsed()Returns the elapsed time since clock initialization.Returns the duration elapsed since this clock was initialized.static MonotonicClockget()Returns the singleton instance of MonotonicClock.getZone()Returns the zone ID of this clock, which is always UTC.instant()Returns a monotonically increasing instant.static Instantnow()Returns the current instant from the monotonic clock.static Instantstart()Returns the initialization time of this monotonic clock.Returns the wall clock time captured when this monotonic clock was initialized.Returns this clock since timezone adjustments are not supported.Methods inherited from class java.time.Clockequals, fixed, hashCode, millis, offset, system, systemDefaultZone, systemUTC, tick, tickMillis, tickMinutes, tickSeconds
- 
Method Details- 
getReturns the singleton instance of MonotonicClock.- Returns:
- the monotonic clock instance
 
- 
nowReturns the current instant from the monotonic clock. This is a convenience method equivalent toget().instant().- Returns:
- the current instant using monotonic timing
 
- 
startReturns the initialization time of this monotonic clock. This is a convenience method equivalent toget().start().- Returns:
- the instant when this monotonic clock was initialized
- See Also:
 
- 
elapsedReturns the elapsed time since clock initialization. This is a convenience method equivalent toget().elapsedTime().- Returns:
- the duration since clock initialization
 
- 
instantReturns a monotonically increasing instant.The returned instant is calculated by adding the elapsed nanoseconds since clock creation to the initial wall clock time. This ensures that the time never goes backwards and maintains a consistent relationship with the wall clock time. - Specified by:
- instantin interface- InstantSource
- Specified by:
- instantin class- Clock
- Returns:
- the current instant using monotonic timing
 
- 
startInstantReturns the wall clock time captured when this monotonic clock was initialized.This instant serves as the base time from which all subsequent instant()calls are calculated by adding the elapsed monotonic duration. This ensures consistency between the monotonic measurements and wall clock time.- Returns:
- the initial wall clock instant when this clock was created
- See Also:
 
- 
elapsedTimeReturns the duration elapsed since this clock was initialized.The returned duration is calculated using System.nanoTime()to ensure monotonic behavior. This duration represents the exact time span between clock initialization and the current instant.- Returns:
- the duration since clock initialization
- See Also:
 
- 
getZone
- 
withZoneReturns this clock since timezone adjustments are not supported.This implementation maintains UTC time to ensure monotonic behavior. The provided zone parameter is ignored. - Specified by:
- withZonein interface- InstantSource
- Specified by:
- withZonein class- Clock
- Parameters:
- zone- the target timezone (ignored)
- Returns:
- this clock instance
 
 
-