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);
}
}
Il Napoli batte il Milan 1-0: Politano firma il sorpasso
-
Il Napoli di Antonio Conte conquista una vittoria pesantissima al
"Maradona", battendo 1-0 il Milan e scavalcandolo al secondo posto in
classifica. Gara bl...
2 ore fa


Nessun commento:
Posta un commento