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);
}
}
Andy Burnham si avvicina alla guida del Labour e potrebbe sostituire
Starmer come primo ministro
-
Punti chiave Il politico Andy Burnham, favorito per succedere a Keir
Starmer come leader del Partito Laburista britannico, ha ricevuto
l'adesione di un imp...
4 giorni fa


Nessun commento:
Posta un commento