java - 使用ntp获取网络时间服务器
问题描述
我正在尝试在 Net-beans IDE 8.2 上使用 ntp 从互联网上获取时间。我没有收到错误,但程序继续运行而不显示时间。
我已经在库中添加了 commons-net-3.6.jar。我错过了什么吗?
请在下面查看我的代码。我已从以下代码中获取此代码:如何使用 Internet 时间服务器获取时间?
package timelookup;
import java.util.Date;
import java.net.InetAddress;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class TimeLookup {
public static void main(String[] args)throws Exception {
String TIME_SERVER = "time-a.nist.gov";
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
long returnTime = timeInfo.getReturnTime();
Date time = new Date(returnTime);
System.out.println("Time from " + TIME_SERVER + ": " + time);
}
}
解决方案
推荐阅读
- jsf - 单击拒绝时取消选中所有其他复选框,如果选中其他复选框则取消选中拒绝
- javascript - 如何处理非常长的数组
- ubuntu - 如何修复“ImportError:libgfortran.so.5:无法打开共享对象文件:没有这样的文件或目录。” 在 ubuntu 18.04 aarch64
- flutter - 如何在 Flutter 上使用 OneSingal 过滤某些通知?
- mingw-w64 - 如何为linux项目构建一个window可执行bin
- sql - 可以在选定的行之后看到前一行吗?
- python - 最初启动表单时如何隐藏组合框或标签
- python-3.x - 如何解决错误:时间数据 'False' 与格式 '%Y-%m-%d %H:%M:%S' 不匹配
- python - 将指示文档中存在的主题的数组转换为枚举主题的元组
- tcl - 识别文本文件中的空行并在 tcl 中使用该列表循环