When you do a system pair, you need to use NTP to get the time.
You can use the Common-net package to get the time of the NTP server (that is, when you can pair to a standard time server, or you can set up an NTP server for yourself).
Time to get NTP using Java (T1,T2,T3,T4). The following is an example of using common-net on the NTP section on the official website.
Very detailed, very helpful.
1 public class Test {2 3 private static final NumberFormat NumberFormat = new Java.text.DecimalFormat ("0.00"); 4 public static String ServerIP = "124.193.130.182"; 5 6 public static final void main (string[] args) throws IOException 7 {8 9 ntpudpclient C lient = new Ntpudpclient (); //We want to timeout if a response takes longer than seconds one client.setdefaulttimeout (10 000); try {client.open (); InetAddress hostaddr = Inetaddress.getbyname (Ser Verip); System.out.println (">" + hostaddr.gethostname () + "/" + hostaddr.gethostaddress ()); Timeinfo info = client.gettime (hostaddr); ProcessResponse (info); catch (SocketException e) {e.printstacktrace (); + Clien T.close (); The public static void ProcessResponse (Timeinfo info)-{Ntpv3packet message = Info.getmessage (); int stratum = Message.gets Tratum (); String RefType; if (stratum <= 0) to RefType = "(Unspecified or unavailable)"; + Else if (stratum = = 1) reftype = "(Primary Reference; e.g., GPS) "; GPS, Radio clock, etc. RefType = "(Secondary Reference; e.g. via NTP or SNTP) "; //stratum should be 0..15 ... PNS System.out.println ("stratum:" + stratum + "+ reftype); $ int Version = Message.getversion (); Message.getleapindicator int li = (); System.out.println ("leap=" + Li + ", version=" + version + ", precision=" + Message . Getprecision ()); System.out.println ("mode:" + message.getmodename () + "(" + message.getmode () + ")"); Poll int = Message.getpoll (); 44 Poll value typically btwn Minpoll (4) and Maxpoll ("System.out.println:" + (Poll poll ; = 0? 1: (int) Math.pow (2, poll)) + "Seconds" + "(2 * * + poll +") "); Double disp = message.getrootdispersioninmillisdouble (); System.out.println ("rootdelay=" + Numberformat.format (message.getrootdelayinmillisdouble ()) 49 + ", Rootdispersion (ms):" + Numberformat.format (disp)); int refId = Message.getreferenceid (); Wuyi String refaddr = ntputils.gethostaddress (refId); RefName String = null; if (refId! = 0) {refaddr.equals ("127.127.1.0") {refName = "LOCAL"; This is the ref address for the Local Clock stratum} else if (>= 2) {57/// If reference ID have 127.127 prefix then it uses its own reference clock-//defined in THe form 127.127.clock-type.unit-num (e.g 127.127.8.0 mode 5//For GENERIC DCF77 AM; see Refclock. HTM from the NTP software distribution. if (!refaddr.startswith ("127.127")) {62 InetAddress addr = Inetaddress.getbyname (REFADDR); The String name = Addr.gethostname (); if (name! = null &&!name.equals (REFADDR)) Refna me = name; (Unknownhostexception e) {//some stratum-2 servers syn c to ref clock device but fudge stratum level higher ... (e.g. 2)//ref not valid host Maybe it s a reference clock name? otherwise//Just show the ref IP address. RefName = ntputils.getreferenceclock (message); 71} 72 } (Version >= 3 && (stratum = 0 | | stratum = = 1)) {74 RefName = ntputils.getreferenceclock (message); //RefName usually has at least 3 characters (e.g. GPS, WWV, LCL, etc.) 76} 77 Otherwise give up on naming the beast ... (RefName! = null && refname.length () > 1) refaddr + = "( "+ RefName +") "; Bayi System.out.println ("Reference identifier:\t" + refaddr); TimeStamp refntptime = Message.getreferencetimestamp (); System.out.println ("Reference timestamp:\t" + refntptime + "" + refntptime.todatestring ()); Originate time is time request sent by client (t1) TimeStamp origntptime = Message.getori Ginatetimestamp (); System.out.println ("Originate timestamp:\t" + origntptime + "+ Origntptime.todatestriNg ()); A long desttime = Info.getreturntime (); //Receive time is time request received by server (t2) TimeStamp rcvntptime = Message.getre Ceivetimestamp (); System.out.println ("Receive timestamp:\t" + rcvntptime + "" + rcvntptime.todatestring ()); Transmit time is time reply sent by server (T3) TimeStamp xmitntptime = Message.gettransm Ittimestamp (); System.out.println ("Transmit timestamp:\t" + Xmitntptime + "" + xmitntptime.todatestring ()); 94//Destination time is time reply received by client (T4) TimeStamp destntptime = TimeStamp. Getntptime (Desttime); System.out.println ("Destination timestamp:\t" + destntptime + "" + destntptime.todatestring ()); Info.computedetails (); Compute Offset/delay If not already done 98 Long Offsetvalue = Info.getoffset (); Delayvalue Long = Info.getdelay (); 100 String delay = (Delayvalue = = null)? "N/A": delayvalue.tostring (); 101 String offset = (Offsetvalue = = null)? "N/A": offsetvalue.tostring (); 102 System.out.println ("Roundtrip delay (ms) =" + delay103 + ", clock offset (ms) =" + offset); Offset in ms104}105 106}
Use NTP to get network time-----java