According to NTPUDPClient javadoc:
To use the class, just open the local dataset open and call getTime() to get the time. Then call close to close the connection correctly. Successive getTime calls are resolved without reconnecting.
You are missing the open() call in your code (technically close() , but that is not the cause of your problem).
In addition, in your requirements, you indicate what you need in real time (and not the time that the local client can set). This value should not be obtained directly, but as the offset necessary to match the local time to the server (remote) time obtained by the getOffset() method.
If real-time acquisition is a common procedure, you can only use NTP once at the beginning and use the resulting bias to correct the system time in the future, reducing the latency of receiving real-time.
Such a process can be described in the class as such:
public class TimeKeeper{ // Constant: Time Server private final String TIME_SERVER = "time-a.nist.gov"; // Last time the time offset was retrieved via NTP private long last_offset_time = -1; // The real time, calculated from offsets, of when the last resync happened private long retrieve_time; private synchronized void resync(){ NTPUDPClient timeClient = new NTPUDPClient(); InetAddress inetAddress = InetAddress.getByName(TIME_SERVER); TimeInfo timeInfo = null; try{ timeClient.open(); timeInfo = timeClient.getTime(inetAddress); }catch(IOException ex){ return; }finally{ timeClient.close(); } // Real time calculated from the offset time and the current system time. retrieve_time = System.currentTimeMillis() + timeInfo.getOffset(); last_offset_time = System.nanoTime(); } public long getRealTimeInMillis(){ // Possible to set some resync criteria here if(last_offset_time == -1){ resync(); // Handle exception whilst retrieving time here } // Returns the system time, corrected with the offset return retrieve_time + Math.round((System.nanoTime() - last_offset_time) / 1000.0) } }
source share