[ACCEPTED]-Java NTP client-ntp

Accepted answer
Score: 11

there is a NTP Java implementation on support.ntp.org


Score: 10

Since this question ranks very high on Google 11 for "Java NTP client":

Android 10 has a nice, compact Apache-licensed implementation 9 of a Simple NTP client: android.net.SntpClient.

It would need to 8 be modified to run on non-Android java, but 7 this should be trivial, since the code is 6 about 100 lines of code plus another 100 5 lines of detailed comments, with no external 4 dependencies except the Android system clock 3 - and by chance, it is ready to accept relative 2 timers like nanoTime() since it already uses such 1 a relative timer.

Score: 4

I have written a java implementation (Java 1 7) that you can use: SntpClient

Score: 3

If you're using Java 5 or above, can you 5 use System.nanoTime() to perform a more accurate measurement 4 of time offsets ? You'd have to modify your 3 existing NTP code, but you should have the 2 source for it, and I wouldn't expect this 1 to be difficult.

Score: 3

I know this is an old question but I notice 12 that all the answers does not explain how 11 to use those libraries. In this answer we 10 will show a basic implementation and its 9 logic behind.

A simple way to implement it 8 is using Apache Commons Net library. This library will provide a NTPUDPClient class 7 to manage connectionless NTP requests and 6 return a TimeInfo instance. This instance should 5 compute the offset between your system's 4 time and the NTP server's time. Lets try 3 to implement it here:

  1. Add the Apache Commons Net library to your project.
  1. Create a new instance of the NTPUDPClient class.
  2. Setup the default timeout and the InetAddress of the NTP Server.
  3. Call the NTPUDPClient.getTime() method to retrieve a TimeInfo instance with the time information from the specified server.
  4. Call computeDetails() method to compute and validate details of the NTP message packet.
  5. Finally, get the offset and calculate an atomic time.

Here we have a basic 2 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

    InetAddress inetAddress = InetAddress.getByName(SERVER_NAME);
    TimeInfo timeInfo = client.getTime(inetAddress);
    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 1 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

More Related questions