请问 谁做过 NTP 时间校正 我想问如何获得 NTP时间 如题 ,网上 两篇文章 写的挺好 代码白天测正常 晚上他那个代码和正常时间 差八个小时 所以求获得ntp 时间的代码 谢谢了java 拜托了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 8 个小时是北京时间与 GMT 时间差,加上时区差不就可以了么? 源程序 都加了 1.import java.io.IOException; 2.import java.io.InterruptedIOException; 3.import java.net.ConnectException; 4.import java.net.DatagramPacket; 5.import java.net.DatagramSocket; 6.import java.net.InetAddress; 7.import java.net.NoRouteToHostException; 8.import java.net.UnknownHostException; 9. 10.public class TestNtp{ 11. 12. public static void main(String[] args){ 13. int retry = 2; 14. int port = 123; 15. int timeout = 3000; 16. 17. // get the address and NTP address request 18. // 19. InetAddress ipv4Addr = null; 20. try { 21. ipv4Addr = InetAddress.getByName("203.117.180.36");//更多NTP时间服务器参考附注 22. } catch (UnknownHostException e1) { 23. e1.printStackTrace(); 24. } 25. 26. int serviceStatus = -1; 27. DatagramSocket socket = null; 28. long responseTime = -1; 29. try { 30. socket = new DatagramSocket(); 31. socket.setSoTimeout(timeout); // will force the 32. // InterruptedIOException 33. 34. for (int attempts = 0; attempts <= retry && serviceStatus != 1; attempts++) { 35. try { 36. // Send NTP request 37. // 38. byte[] data = new NtpMessage().toByteArray(); 39. DatagramPacket outgoing = new DatagramPacket(data, data.length, ipv4Addr, port); 40. long sentTime = System.currentTimeMillis(); 41. socket.send(outgoing); 42. 43. // Get NTP Response 44. // 45. // byte[] buffer = new byte[512]; 46. DatagramPacket incoming = new DatagramPacket(data, data.length); 47. socket.receive(incoming); 48. responseTime = System.currentTimeMillis() - sentTime; 49. double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0; 50. //这里要加2208988800,是因为获得到的时间是格林尼治时间,所以要变成东八区的时间,否则会与与北京时间有8小时的时差 51. 52. // Validate NTP Response 53. // IOException thrown if packet does not decode as expected. 54. NtpMessage msg = new NtpMessage(incoming.getData()); 55. double localClockOffset = ((msg.receiveTimestamp - msg.originateTimestamp) + (msg.transmitTimestamp - destinationTimestamp)) / 2; 56. 57. System.out.println("poll: valid NTP request received the local clock offset is " + localClockOffset + ", responseTime= " + responseTime + "ms"); 58. System.out.println("poll: NTP message : " + msg.toString()); 59. serviceStatus = 1; 60. } catch (InterruptedIOException ex) { 61. // Ignore, no response received. 62. } 63. } 64. } catch (NoRouteToHostException e) { 65. System.out.println("No route to host exception for address: " + ipv4Addr); 66. } catch (ConnectException e) { 67. // Connection refused. Continue to retry. 68. e.fillInStackTrace(); 69. System.out.println("Connection exception for address: " + ipv4Addr); 70. } catch (IOException ex) { 71. ex.fillInStackTrace(); 72. System.out.println("IOException while polling address: " + ipv4Addr); 73. } finally { 74. if (socket != null) 75. socket.close(); 76. } 77. 78. // Store response time if available 79. // 80. if (serviceStatus == 1) { 81. System.out.println("responsetime=="+responseTime); 82. } 83. 84. 85. } 86.} 他有个 System.out.println("poll: NTP message : " + msg.toString()); 输出一堆poll: valid NTP request received the local clock offset is 3606.92320227623, responseTime= 265ms poll: NTP message : Leap indicator: 0 Version: 3 Mode: 4 Stratum: 1 Poll: 0 Precision: -18 (3.8E-6 seconds) Root delay: 0.00 ms Root dispersion: 0.00 ms Reference identifier: ACTS Reference timestamp: 26-三月-2009 20:50:23.508540 Originate timestamp: 26-三月-2009 19:51:10.031000 Receive timestamp: 26-三月-2009 20:51:17.086693 Transmit timestamp: 26-三月-2009 20:51:17.086712 responsetime==265 东西不之 故不固定 如何结串地 49. double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0; 50. //这里要加2208988800,是因为获得到的时间是格林尼治时间,所以要变成东八区的时间,否则会与与北京时间有8小时的时差 他家的 但是 晚上 执行就有问题总结下 两点 输出一堆 1 不知如何 结串 2 晚上这段代码 差八个小时 原作者 还添加了 时区那个东东我 时在是不会了 求 帮助 求获取 nTP时间的代码//************************************************************************************************************** 有些代码 没写 http://lshh83.iteye.com/blog/546405 就是这个 网上都是这个 的 没有别的了谁知道那个 大哥是原创 转来转去 抄来抄去 为什么list添加的是同一个对象?各位大虾来帮帮忙! IE出现问题。 s2sh整合出错 hibernate 错误 axis1.4 返回数组,出现异常 关于js和struts的问题 jTable滚动条的问题 有人用JAAS吗?近来讨论一下! 年底不好找工作呀。 系统同时获取MySQL Access数据 spring属性注入失败 java如果判断一个字符串中是否有乱码
2.import java.io.InterruptedIOException;
3.import java.net.ConnectException;
4.import java.net.DatagramPacket;
5.import java.net.DatagramSocket;
6.import java.net.InetAddress;
7.import java.net.NoRouteToHostException;
8.import java.net.UnknownHostException;
9.
10.public class TestNtp{
11.
12. public static void main(String[] args){
13. int retry = 2;
14. int port = 123;
15. int timeout = 3000;
16.
17. // get the address and NTP address request
18. //
19. InetAddress ipv4Addr = null;
20. try {
21. ipv4Addr = InetAddress.getByName("203.117.180.36");//更多NTP时间服务器参考附注
22. } catch (UnknownHostException e1) {
23. e1.printStackTrace();
24. }
25.
26. int serviceStatus = -1;
27. DatagramSocket socket = null;
28. long responseTime = -1;
29. try {
30. socket = new DatagramSocket();
31. socket.setSoTimeout(timeout); // will force the
32. // InterruptedIOException
33.
34. for (int attempts = 0; attempts <= retry && serviceStatus != 1; attempts++) {
35. try {
36. // Send NTP request
37. //
38. byte[] data = new NtpMessage().toByteArray();
39. DatagramPacket outgoing = new DatagramPacket(data, data.length, ipv4Addr, port);
40. long sentTime = System.currentTimeMillis();
41. socket.send(outgoing);
42.
43. // Get NTP Response
44. //
45. // byte[] buffer = new byte[512];
46. DatagramPacket incoming = new DatagramPacket(data, data.length);
47. socket.receive(incoming);
48. responseTime = System.currentTimeMillis() - sentTime;
49. double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0;
50. //这里要加2208988800,是因为获得到的时间是格林尼治时间,所以要变成东八区的时间,否则会与与北京时间有8小时的时差
51.
52. // Validate NTP Response
53. // IOException thrown if packet does not decode as expected.
54. NtpMessage msg = new NtpMessage(incoming.getData());
55. double localClockOffset = ((msg.receiveTimestamp - msg.originateTimestamp) + (msg.transmitTimestamp - destinationTimestamp)) / 2;
56.
57. System.out.println("poll: valid NTP request received the local clock offset is " + localClockOffset + ", responseTime= " + responseTime + "ms");
58. System.out.println("poll: NTP message : " + msg.toString());
59. serviceStatus = 1;
60. } catch (InterruptedIOException ex) {
61. // Ignore, no response received.
62. }
63. }
64. } catch (NoRouteToHostException e) {
65. System.out.println("No route to host exception for address: " + ipv4Addr);
66. } catch (ConnectException e) {
67. // Connection refused. Continue to retry.
68. e.fillInStackTrace();
69. System.out.println("Connection exception for address: " + ipv4Addr);
70. } catch (IOException ex) {
71. ex.fillInStackTrace();
72. System.out.println("IOException while polling address: " + ipv4Addr);
73. } finally {
74. if (socket != null)
75. socket.close();
76. }
77.
78. // Store response time if available
79. //
80. if (serviceStatus == 1) {
81. System.out.println("responsetime=="+responseTime);
82. }
83.
84.
85. }
86.}
输出一堆
poll: valid NTP request received the local clock offset is 3606.92320227623, responseTime= 265ms
poll: NTP message : Leap indicator: 0 Version: 3 Mode: 4 Stratum: 1 Poll: 0 Precision: -18 (3.8E-6 seconds) Root delay: 0.00 ms Root dispersion: 0.00 ms Reference identifier: ACTS Reference timestamp: 26-三月-2009 20:50:23.508540 Originate timestamp: 26-三月-2009 19:51:10.031000 Receive timestamp: 26-三月-2009 20:51:17.086693 Transmit timestamp: 26-三月-2009 20:51:17.086712
responsetime==265 东西不之 故不固定 如何结串
地 49. double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0;
50. //这里要加2208988800,是因为获得到的时间是格林尼治时间,所以要变成东八区的时间,否则会与与北京时间有8小时的时差 他家的 但是 晚上 执行就有问题
总结下 两点 输出一堆 1 不知如何 结串 2 晚上这段代码 差八个小时 原作者 还添加了 时区那个东东我 时在是不会了 求 帮助 求获取 nTP时间的代码//************************************************************************************************************** 有些代码 没写 http://lshh83.iteye.com/blog/546405 就是这个 网上都是这个 的 没有别的了谁知道那个 大哥是原创 转来转去 抄来抄去