需要 commons-net-3.4.jar 库。

import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.InetAddress.*;
import java.util.Date;
import java.util.Calendar;
import org.apache.commons.net.ntp.TimeStamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.io.IOException; public class t {
public static void main(String[] args) {
try {
NTPUDPClient timeClient = new NTPUDPClient();
String timeServerUrl = "24.56.178.140";
InetAddress timeServerAddress = InetAddress.getByName(timeServerUrl);
TimeInfo timeInfo = timeClient.getTime(timeServerAddress);
TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(dateFormat.format(timeStamp.getDate()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

npt服务器的地址大概有:

http://tf.nist.gov/tf-cgi/servers.cgi

1.cn.pool.ntp.org

2.cn.pool.ntp.org

3.cn.pool.ntp.org

0.cn.pool.ntp.org

cn.pool.ntp.org

tw.pool.ntp.org

0.tw.pool.ntp.org

1.tw.pool.ntp.org

2.tw.pool.ntp.org

3.tw.pool.ntp.org

java 通过网络 ntp 获取网络时间的更多相关文章

  1. Unity通过NTP获取网络时间

    最初通过qq时间服务器获得时间,经常出现有网络也获取失败的情况. 后面寻找解决办法,查找资料终于发现通过ntp时间服务器获取网络时间的方法.   首先游戏开始获得初始化网络时间,通常只获取一次,其他时 ...

  2. java代码--Date类获取当前时间-格式化输出

    44:52 阅读数:2299 package cn.Date;   import java.text.Format; import java.text.SimpleDateFormat; import ...

  3. java获取当前日期时间代码总结

    1.获取当前时间,和某个时间进行比较.此时主要拿long型的时间值.  方法如下: 要使用 java.util.Date .获取当前时间的代码如下  代码如下 复制代码 Date date = new ...

  4. Android 获取系统时间和网络时间

    有些时候我们的应用中只能使用网络时间,而不能使用系统的时间,这是为了避免用户关闭了使用网络时间的功能后所产生的误差. 直接上代码. 1.清单文件中网络添加权限. <!-- 访问Internet资 ...

  5. Android获取网络时间的方法

    一.通过免费或者收费的API接口获取 1.免费 QQ:http://cgi.im.qq.com/cgi-bin/cgi_svrtime 淘宝:http://api.m.taobao.com/rest/ ...

  6. ESP8266- 使用AT指令获取网络时间

    前言:很早就考虑过用 ESP8266 获取网络时间,以前都是用 ESP8266 刷机智云的 Gagent 固件,但无奈现在手头的 ESP-01 的 Flash 只有 1M,实在无法胜任.经过在网络上的 ...

  7. Java 网络爬虫获取网页源代码原理及实现

    Java 网络爬虫获取网页源代码原理及实现 1.网络爬虫是一个自动提取网页的程序,它为搜索引擎从万维网上下载网页,是搜索引擎的重要组成.传统爬虫从一个或若干初始网页的URL开始,获得初始网页上的URL ...

  8. paip.uapi 获取网络url内容html 的方法java php ahk c++ python总结.

    paip.uapi 获取网络url内容html 的方法java php ahk c++ python总结. 各种语言总结比较,脚本php.python果然是方便.简短,实用. uapi : get_w ...

  9. Android(java)学习笔记204:自定义SmartImageView(继承自ImageView,扩展功能为自动获取网络路径图片)

    1.有时候Android系统配置的UI控件,不能满足我们的需求,Android开发做到了一定程度,多少都会用到自定义控件,一方面是更加灵活,另一方面在大数据量的情况下自定义控件的效率比写布局文件更高. ...

随机推荐

  1. Mesh Baker的基本操作与功能演示

    原地址:http://www.narkii.com/club/thread-301789-1.html 如何降低游戏在系统中的消耗并带给用户最佳的体验是开发者一直追求的目标,在Unity里面对于模型与 ...

  2. Stretch a row if data overflows in jasper reports

    My band stretches if necessary with the following conditions, I do not know yours. I have started wi ...

  3. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. 1989-C. 数字三角形

    描述 如图所示,是一个数字搭成的三角形. 若起始位置在三角形的顶端,结束位置在三角形底边,每一步只能向下方或向右下角移动一格.请编程计算一条路径,使得路径上经过的数字和最大.(图中路径7→3→8→7→ ...

  5. Google chrome的字体设置

    http://blog.sina.com.cn/s/blog_a3b863da01016sv3.html 谷歌浏览器(Google chrome)速度很快,很好用.问题是字体显示有时候不对:用英文版的 ...

  6. Android 显示大图片

    主要的代码如下: BitmapFactory.Options options = new BitmapFactory.Options(); //图片解析配置 options.inJustDecodeB ...

  7. IText PdfPTable表格 单元的居中显示

    昨晚寻找了网上很多关于IText表格居中问题,他们其中的有些代码我即使复制上去生成的doc表格的文字都是不居中的,后来我自己找出了一种居中方式: 为PdfPCell对象添加paragraph对象,并将 ...

  8. POJ1159——Palindrome(最长公共子序列+滚动数组)

    Palindrome DescriptionA palindrome is a symmetrical string, that is, a string read identically from ...

  9. POJ2993——Emag eht htiw Em Pleh(字符串处理+排序)

    Emag eht htiw Em Pleh DescriptionThis problem is a reverse case of the problem 2996. You are given t ...

  10. AIDL与stub

     Stub翻译成中文是存根的意思,注意Stub对象是在被调用端进程,也就是服务端进程,至此,服务端aidl服务端得编码完成了.  stub是为了方便client,service交互而生成出来的代码.A ...