标签:
在做系统对时的时候,需要使用到ntp来获取时间。
可以使用common-net包来获取ntp服务器的时间(即可以向那些标准时间服务器对时,也可以向自己设置好的ntp服务器进行对时)。
使用java获取ntp的时间(t1,t2,t3,t4)。下面是官网上给出的关于使用common-net关于ntp部分的使用例子。
很详细,很有帮助。
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 client = new NTPUDPClient();
10 // We want to timeout if a response takes longer than 10 seconds
11 client.setDefaultTimeout(10000);
12 try {
13 client.open();
14 InetAddress hostAddr = InetAddress.getByName(ServerIP);
15 System.out.println(" > " + hostAddr.getHostName() + "/" + hostAddr.getHostAddress());
16 TimeInfo info = client.getTime(hostAddr);
17 processResponse(info);
18 } catch (SocketException e) {
19 e.printStackTrace();
20 }
21 client.close();
22 }
23
24
25 public static void processResponse(TimeInfo info)
26 {