Se nelle nostre applicazioni Java abbiamo bisogno di interrogare l'Internet Time Service di un Time Server, possiamo utilizzare il Network Time Protocol (NTP), mediante la libreria open source Apache Commons Net.
Vediamone un esempio di utilizzo:
package test;
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 {
// List of time servers: http://tf.nist.gov/service/time-servers.html
// Do not query time server more than once every 4 seconds
public static final String TIME_SERVER = "time-a.nist.gov";
public static void main(String[] args) throws Exception {
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);
}
}
Come svuotare la cache DNS su Windows 11 e 10: guida semplice
-
Ogni giorno navighiamo sul web digitando indirizzi testuali senza pensare
al complesso meccanismo che trasforma quelle parole nei siti grafici che
visual...
19 ore fa


Nessun commento:
Posta un commento