ntp服务器 时间(查看服务器时间)

2022-07-28 18:32:34 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript复制
public static String getNTPServerDateTime(String NTPTimeServerUrl) {
    String dateTimeMessage;
    try {
        NTPUDPClient timeClient = new NTPUDPClient();
        InetAddress timeServerAddress = InetAddress.getByName(NTPTimeServerUrl);
        TimeInfo timeInfo = timeClient.getTime(timeServerAddress);
        TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
        Date date = timeStamp.getDate();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        dateTimeMessage = dateFormat.format(date);
    } catch (UnknownHostException e) {
        return e.getMessage();
    } catch (IOException e) {
        return e.getMessage();
    }
    return dateTimeMessage;
}NTPTimeServerUrl为NTP服务器地址,如上海交通大学网络中心NTP服务器地址为:202.120.2.101           

0 人点赞