How to use an Internet time server to get the time?
sp0d is not quite right:
timeInfo.getReturnTime(); // Returns time at which time message packet was received by local machine
So it just returns current system time, not the received one. See TimeInfo man page.
You should use
timeInfo.getMessage().getTransmitTimeStamp().getTime();
instead.
So the code block will be:
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);
Here is a code i found somewhere else.. and i am using it. Uses apache commons library.
List of time servers: NIST Internet Time Service
import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class TimeLookup {
public static void main() throws Exception {
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getReturnTime();
Date time = new Date(returnTime);
System.out.println("Time from " + TIME_SERVER + ": " + time);
}
}
Returns the output
Time from time-d.nist.gov
: Sun Nov 25 06:04:34 IST 2012
I know this is an old question but I notice that all the answers are not correct or are complicated.
A nice and simple way to implement it is using Apache Commons Net library. This library will provide a NTPUDPClient
class to manage connectionless NTP requests. This class will return a TimeInfo
instance. This object should run the compute method to calculate the offset between your system's time and the NTP server's time. Lets try to implement it here
- Add the Apache Commons Net library to your project.
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
- Create a new instance of the
NTPUDPClient
class. - Setup the default timeout
- Get the
InetAddress
of the NTP Server. - Call the
NTPUDPClient.getTime()
method to retrieve aTimeInfo
instance with the time information from the specified server. - Call the computeDetails() method to compute and validate details of the NTP message packet.
- Finally, get a NTP timestamp object based on a Java time by using this code
TimeStamp.getNtpTime(currentTime + offset).getTime()
.
Here we have a basic implementation:
import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class NTPClient {
private static final String SERVER_NAME = "pool.ntp.org";
private volatile TimeInfo timeInfo;
private volatile Long offset;
public static void main() throws Exception {
NTPUDPClient client = new NTPUDPClient();
// We want to timeout if a response takes longer than 10 seconds
client.setDefaultTimeout(10_000);
InetAddress inetAddress = InetAddress.getByName(SERVER_NAME);
TimeInfo timeInfo = client.getTime(inetAddress);
timeInfo.computeDetails();
if (timeInfo.getOffset() != null) {
this.timeInfo = timeInfo;
this.offset = timeInfo.getOffset();
}
// This system NTP time
TimeStamp systemNtpTime = TimeStamp.getCurrentTime();
System.out.println("System time:\t" + systemNtpTime + " " + systemNtpTime.toDateString());
// Calculate the remote server NTP time
long currentTime = System.currentTimeMillis();
TimeStamp atomicNtpTime = TimeStamp.getNtpTime(currentTime + offset).getTime()
System.out.println("Atomic time:\t" + atomicNtpTime + " " + atomicNtpTime.toDateString());
}
public boolean isComputed()
{
return timeInfo != null && offset != null;
}
}
You will get something like that:
System time: dfaa2c15.2083126e Thu, Nov 29 2018 18:12:53.127
Atomic time: dfaa2c15.210624dd Thu, Nov 29 2018 18:12:53.129