网上找到了
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NoRouteToHostException;
import java.net.UnknownHostException;
public class TestNtp {  public static void main(String[] args){
        int retry = 2;
        int port = 123;
        int timeout = 3000;         // get the address and NTP address request
        //
        InetAddress ipv4Addr = null;
        try {
            ipv4Addr = InetAddress.getByName("203.117.180.36");//更多NTP时间服务器参考附注
               } catch (UnknownHostException e1) {
            e1.printStackTrace();
        }         int serviceStatus = -1;
        DatagramSocket socket = null;
        long responseTime = -1;
        try {
            socket = new DatagramSocket();
            socket.setSoTimeout(timeout); // will force the
            // InterruptedIOException             for (int attempts = 0; attempts <= retry && serviceStatus != 1; attempts++) {
                try {
                    // Send NTP request
                    //
                    byte[] data = new NtpMessage().toByteArray();
                    DatagramPacket outgoing = new DatagramPacket(data, data.length, ipv4Addr, port);
                    long sentTime = System.currentTimeMillis();
                    socket.send(outgoing);                     // Get NTP Response
                    //
                    // byte[] buffer = new byte[512];
                    DatagramPacket incoming = new DatagramPacket(data, data.length);
                    socket.receive(incoming);
                    responseTime = System.currentTimeMillis() - sentTime;
                    double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0;
                    //这里要加2208988800,是因为获得到的时间是格林尼治时间,所以要变成东八区的时间,否则会与与北京时间有8小时的时差                     // Validate NTP Response
                    // IOException thrown if packet does not decode as expected.
                    NtpMessage msg = new NtpMessage(incoming.getData());
                    double localClockOffset = ((msg.receiveTimestamp - msg.originateTimestamp) + (msg.transmitTimestamp - destinationTimestamp)) / 2;                    // System.out.print("===" + localClockOffset + ", responseTime= " + responseTime + "ms");
                  System.out.println("poll: NTP message : " + msg.toString());
                    serviceStatus = 1;
                } catch (InterruptedIOException ex) {
                    // Ignore, no response received.
                }
            }
        } catch (NoRouteToHostException e) {
            System.out.println("No route to host exception for address: " + ipv4Addr);
        } catch (ConnectException e) {
            // Connection refused. Continue to retry.
            e.fillInStackTrace();
            System.out.println("Connection exception for address: " + ipv4Addr);
        } catch (IOException ex) {
            ex.fillInStackTrace();
            System.out.println("IOException while polling address: " + ipv4Addr);
        } finally {
            if (socket != null)
                socket.close();
        }         // Store response time if available
        //
        if (serviceStatus == 1) {
            System.out.println("responsetime=="+responseTime);
        }         
    }
}

解决方案 »

  1.   

    poll: NTP message : Leap indicator: 0 Version: 3 Mode: 0 Stratum: 0 Poll: 0 Precision: 0 (1E0 seconds) Root delay: 0.00 ms Root dispersion: 0.00 ms Reference identifier: imestamp: 0 Receive timestamp:   19-十二月-2011 11:34:16.891405 Transmit timestamp: 0
    输出是这些 东西 不知如何 获得 2011-12-19 11:34:16 只想要这些 谢谢了
      

  2.   

    double localClockOffset = ((msg.receiveTimestamp - msg.originateTimestamp) + (msg.transmitTimestamp - 看这里貌似这个NtpMessage可能还是有些内建功能,如果有格式转化的就可以直接用了
    没的话可能要借助一些功能类(DateFormat)或自己来实现转化了19-十二月-2011 11:34:16.891405
      

  3.   

    可以先将你得到的时间串做预处理,将中文部分先替换为英文对等物,再转为Date型
    import java.text.Format;
    import java.text.SimpleDateFormat;
    import java.util.Date;public class Main {
      public static void main(String[] argv) throws Exception {    Format formatter = new SimpleDateFormat("dd-MMM-yy");
        Date date = (Date) formatter.parseObject("29-Jan-02");
        System.out.println(date);
      }
    }
    就可以将得到的Date对象继续转化成自己想要的时间格式
    import java.text.SimpleDateFormat;
    import java.util.Date;public class Main {  public static void main(String[] args) {
        SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");    Date now = new Date();    String strDate = sdfDate.format(now);    System.out.println("Date: " + strDate);
      }
    }
    //Date: 18/02/2009
      

  4.   

    System.out.println("poll: NTP message : " + msg.toString());程序是输出这个得到的
    输出内容是
    poll: NTP message : Leap indicator: 0 Version: 3 Mode: 0 Stratum: 0 Poll: 0 Precision: 0 (1E0 seconds) Root delay: 0.00 ms Root dispersion: 0.00 ms Reference identifier: imestamp: 0 Receive timestamp: 19-十二月-2011 11:34:16.891405 Transmit timestamp: 0一堆这个东西 这我 截串 如何截取呀  得到时间 进行 format 可是这一些这个东西 我如何呀有做过的吗 给些思路  谢谢了 就是 获取ntp 时间 校正 用java 
      

  5.   

    poll: valid NTP request received the local clock offset is -8.113721132278442, responseTime= 407ms
    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: 19-十二月-2011 14:25:04.713140 Originate timestamp: 19-十二月-2011 14:25:39.296000 Receive timestamp:   19-十二月-2011 14:25:31.385768 Transmit timestamp: 19-十二月-2011 14:25:31.385790
    responsetime==407test time 22:25 这是我输出的 差八个小时 原作者也没测试 写的还洋洋得一 抄袭者不明真理 真是 悲哀呀 
    可气的原作者可气的 抄袭者 懂得人不说 ,抄袭者不会 技术 都是垃圾 csdn 也是网上老多陈贴 子没有答案
    就删它 浪费我网速 这样能抱正 在csdn找到答案, 这点营销理念都没有,csdn so 一下 就是90%的正确答案 还抓取没用的干嘛  
      

  6.   

    看了你输出的结果.timestamp: 这个开始之后是时间.你可以从他开始截取到.这段就是时间了.之后在将里面的汉字换成数字,在根据SimpleDataFormat给个格式自己转吧
      

  7.   

    输出的那个 东西 谁知道格式固定 否 在一个 他那个晚上差八个小时已经做了这个操作double destinationTimestamp = (System.currentTimeMillis() / 1000.0) + 2208988800.0;
    //这里要加2208988800,是因为获得到的时间是格林尼治时间,所以要变成东八区的时间,否则会与与北京时间有8小时的时差