大家好,又见面了,我是你们的朋友全栈君。
代码语言: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