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);
}
}
GrAudio edizione delle 15:30 del 3 febbraio
-
GrAudio edizione delle 15:30 del 3 febbraio
Canali: Vercelli
3 ore fa


Nessun commento:
Posta un commento