如何解决Google NTP服务器有时不响应
我正在尝试实现Google(time.google.com)NTP服务器,以获取应用程序的当前互联网时间。 有时它可以工作,有时却不能,这使我的NTP服务器没有发现错误。
public static Date getNtpServerDate(final String timeServerHost) throws IOException {
try {
TimeInfo timeInfo = null;
final NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_SERVER_TIMEOUT_MS);
final InetAddress inetAddress = InetAddress.getByName("time.google.com");
if (inetAddress != null) {
timeInfo = timeClient.getTime(inetAddress);
if (timeInfo != null) {
// TODO: which time to use?
final long serverTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
//long serverTime = timeInfo.getReturnTime();
final Date ntpDate = new Date(serverTime);
return ntpDate;
}
}
} catch (final IOException e) {
throw new IOException("Unable to get NTP server time.",e);
}
return null;
}
这是我用来获取NTP时间的代码 请帮我解决这个问题。有人可以帮我吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。