Class SntpConnector
- All Implemented Interfaces:
TimeSource<Moment>
Connects to a modern time server using the NTP-protocol.
This class needs a socket connection via the port 123. The exact configuration can be set in the constructors but can also be changed at runtime. It is recommended not to connect to the NTP-server during or near a leap second because the NTP-protocol only repeats such a timestamp causing ambivalences.
Note: This implementation is threadsafe.
- Since:
- 2.1
- Author:
- Meno Hochschild
-
Constructor Summary
ConstructorDescriptionCreates a new instance which is configured by aServiceLoader
.SntpConnector(String server)
Creates a new instance which uses a default configuration using the specified NTP4-server.Creates a new instance which is configured by given argument. -
Method Summary
Modifier and TypeMethodDescriptionlong
Returns the current time in microseconds since the Unix epoch [1970-01-01T00:00:00,000000Z].long
Returns the current time in milliseconds since the Unix epoch [1970-01-01T00:00:00,000Z].Returns the last received message of the NTP-server.Methods inherited from class net.time4j.clock.NetTimeConnector
connect, currentTime, getLastConnectionTime, getLastOffsetInMicros, getNetTimeConfiguration, isLogEnabled, isRunning, reconfigure, setLogWriter
Methods inherited from class net.time4j.clock.AbstractClock
inLocalView, inPlatformView, inZonalView, inZonalView
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
-
Constructor Details
-
SntpConnector
public SntpConnector()Creates a new instance which is configured by a
ServiceLoader
.- Throws:
IllegalStateException
- if no configuration could be found- See Also:
ServiceLoader
-
SntpConnector
Creates a new instance which is configured by given argument.
- Parameters:
ntc
- SNTP-configuration
-
SntpConnector
Creates a new instance which uses a default configuration using the specified NTP4-server.
Example:
SntpConnector clock = new SntpConnector("ptbtime1.ptb.de"); clock.connect(); System.out.println(clock.currentTime());
- Parameters:
server
- NTP4-server
-
-
Method Details
-
currentTimeInMillis
public long currentTimeInMillis()Returns the current time in milliseconds since the Unix epoch [1970-01-01T00:00:00,000Z].
UTC leap seconds are never counted.
- Returns:
- count of milliseconds since UNIX-epoch without leap seconds
- Since:
- 2.1
-
currentTimeInMicros
public long currentTimeInMicros()Returns the current time in microseconds since the Unix epoch [1970-01-01T00:00:00,000000Z].
UTC leap seconds are never counted.
- Returns:
- count of microseconds since UNIX-epoch without leap seconds
- Since:
- 2.1
-
getLastReply
Returns the last received message of the NTP-server.
- Returns:
- server message or
null
if not yet received - Since:
- 2.1
- See Also:
NetTimeConnector.connect()
-