Class SystemClock
- All Implemented Interfaces:
TimeSource<Moment>
Represents a clock which is based on the clock of the underlying operating system.
- Author:
- Meno Hochschild
-
Field Summary
Modifier and TypeFieldDescriptionstatic SystemClock
Standard implementation.static SystemClock
Monotonic clock based on the best available clock of the underlying operating system. -
Method Summary
Modifier and TypeMethodDescriptionstatic Moment
Equivalent toSystemClock.INSTANCE.currentTime()
.Yields the current time.long
Yields the current time in microseconds elapsed since [1970-01-01T00:00:00,000000Z].long
Yields the current time in milliseconds elapsed since [1970-01-01T00:00:00,000Z].static ZonalClock
Creates a local clock in system timezone.static ZonalClock
Creates a local clock in the platform timezone.static ZonalClock
inZonalView(String tzid)
Creates a local clock in given timezone.static ZonalClock
inZonalView(TZID tzid)
Creates a local clock in given timezone.long
Yields the current time in microseconds elapsed since UTC epoch [1972-01-01T00:00:00,000000Z].Recalibrates this instance and yields a new copy.synchronizedWith(TimeSource<?> clock)
Synchronizes this instance with given time source and yields a new copy.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.time4j.base.TimeSource
currentInstant
-
Field Details
-
INSTANCE
Standard implementation.
The system property "net.time4j.systemclock.nanoTime" controls if this clock is internally based on the expression
System.nanoTime()
(if property is set to "true") orSystem.currentTimeMillis()
(default). The standard case is a clock which is affected by OS-triggered time jumps and user adjustments so there is no guarantee for a monotonic time. -
MONOTONIC
Monotonic clock based on the best available clock of the underlying operating system.
A side effect of this implementation can be increased nominal precision up to nanoseconds although no guarantee is made to ensure nanosecond accuracy. The accuracy is often limited to milliseconds. However, the main focus and motivation is realizing a monotonic behaviour by delegating to the use of a monotonic clock of the underlying OS. Equivalent to
CLOCK_MONOTONIC
on a Linux-server.- Since:
- 3.2/4.1
- See Also:
TickProvider.getNanos()
-
-
Method Details
-
currentTime
Description copied from interface:TimeSource
Yields the current time.
- Specified by:
currentTime
in interfaceTimeSource<Moment>
- Returns:
- current time in seconds (as
UnixTime
object or derivate)
-
currentTimeInMillis
public long currentTimeInMillis()Yields the current time in milliseconds elapsed since [1970-01-01T00:00:00,000Z].
- Returns:
- count of milliseconds since UNIX epoch without leap seconds
- See Also:
currentTimeInMicros()
-
currentTimeInMicros
public long currentTimeInMicros()Yields the current time in microseconds elapsed since [1970-01-01T00:00:00,000000Z].
If this clock is based only on
System.currentTimeMillis()
then this method will just multiply the millisecond value by factor1000
. On many operating systems the precision is limited to milliseconds. This is even true if this clock is based onSystem.nanoTime()
because for purpose of calibration even here the methodSystem.currentTimeMillis()
must be accessed at least one time.- Returns:
- count of microseconds since UNIX epoch without leap seconds
-
realTimeInMicros
public long realTimeInMicros()Yields the current time in microseconds elapsed since UTC epoch [1972-01-01T00:00:00,000000Z].
- Returns:
- count of microseconds since UTC epoch including leap seconds
- Since:
- 3.2/4.1
- See Also:
currentTimeInMicros()
-
inPlatformView
Creates a local clock in the platform timezone.
Uses the standard clock
SystemClock.INSTANCE
and the platform timezone data.Background: Some mobile devices might have outdated or wrong timezone data but still display the correct local time because they compensate the wrong zone information by adjusting the clock. This method can help in such a situation, for example:
// we assume/know that the local device time is correct... PlainTimestamp localTimestamp = SystemClock.inPlatformView().now(); // ...and then we combine correct local device time with the valid timezone data of Time4J Moment nowCorrect = localTimestamp.inStdTimezone();
- Returns:
- local clock in system timezone using the platform timezone data
- Since:
- 3.3/4.2
- See Also:
Timezone.ofPlatform()
,ZonalClock.now()
,INSTANCE
-
inLocalView
Creates a local clock in system timezone.
Uses the standard clock
SystemClock.INSTANCE
.- Returns:
- cached local clock in system timezone using the best available timezone data
- See Also:
Timezone.ofSystem()
,INSTANCE
-
inZonalView
Creates a local clock in given timezone.
In order to achieve a monotonic zonal clock, users can use the expression
new ZonalClock(SystemClock.MONOTONIC, tzid)
.- Parameters:
tzid
- timezone id- Returns:
- local clock in given timezone
- Throws:
IllegalArgumentException
- if given timezone cannot be loaded- See Also:
INSTANCE
-
inZonalView
Creates a local clock in given timezone.
In order to achieve a monotonic zonal clock, users can use the expression
new ZonalClock(SystemClock.MONOTONIC, tzid)
.- Parameters:
tzid
- timezone id- Returns:
- local clock in given timezone
- Throws:
IllegalArgumentException
- if given timezone cannot be loaded- See Also:
INSTANCE
-
currentMoment
Equivalent to
SystemClock.INSTANCE.currentTime()
.- Returns:
- current time using the standard implementation
- Since:
- 2.3
- See Also:
INSTANCE
-
recalibrated
Recalibrates this instance and yields a new copy.
This method is only relevant if this clock is operated in monotonic mode. It is strongly advised not to recalibrate during or near a leap second. Please also note that this method might cause jumps in time - even backwards.
- Returns:
- new and recalibrated copy of this instance
- Since:
- 3.2/4.1
- See Also:
MONOTONIC
-
synchronizedWith
Synchronizes this instance with given time source and yields a new copy.
This method is only relevant if this clock is operated in monotonic mode. It is strongly advised not to recalibrate during or near a leap second. Please also note that this method might cause jumps in time - even backwards.
- Parameters:
clock
- another clock which this instance should be synchronized with- Returns:
- synchronized copy of this instance
- Since:
- 3.2/4.1
- See Also:
MONOTONIC
-