java获取网络当前时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java获取网络当前时间相关的知识,希望对你有一定的参考价值。
要的是当前网络时间,不要系统时间,获取的速度能快一点的,谁有没有完整的代码和包,发给一份吧,谢了
如果你要获取的是Internet时间,可以使用NTP服务。
NTP概念简介
Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。
java实现:
import java.io.InputStream;import java.net.Socket;
public class TimeUtil
public static final int DEFAULT_PORT = 37;//NTP服务器端口
public static final String DEFAULT_HOST = "time-nw.nist.gov";//NTP服务器地址
private TimeUtil()
;
public static long currentTimeMillis(Boolean sync)
if (sync != null && sync.booleanValue() != true)
return System.currentTimeMillis();
try
return syncCurrentTime();
catch (Exception e)
return System.currentTimeMillis();
public static long syncCurrentTime() throws Exception
// The time protocol sets the epoch at 1900,
// the java Date class at 1970. This number
// converts between them.
long differenceBetweenEpochs = 2208988800L;
// If you'd rather not use the magic number uncomment
// the following section which calculates it directly.
/*
* TimeZone gmt = TimeZone.getTimeZone("GMT"); Calendar epoch1900 =
* Calendar.getInstance(gmt); epoch1900.set(1900, 01, 01, 00, 00, 00);
* long epoch1900ms = epoch1900.getTime().getTime(); Calendar epoch1970
* = Calendar.getInstance(gmt); epoch1970.set(1970, 01, 01, 00, 00, 00);
* long epoch1970ms = epoch1970.getTime().getTime();
*
* long differenceInMS = epoch1970ms - epoch1900ms; long
* differenceBetweenEpochs = differenceInMS/1000;
*/
InputStream raw = null;
try
Socket theSocket = new Socket(DEFAULT_HOST, DEFAULT_PORT);
raw = theSocket.getInputStream();
long secondsSince1900 = 0;
for (int i = 0; i < 4; i++)
secondsSince1900 = (secondsSince1900 << 8) | raw.read();
if (raw != null)
raw.close();
long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
long msSince1970 = secondsSince1970 * 1000;
return msSince1970;
catch (Exception e)
throw new Exception(e);
中国大概能用的NTP时间服务器
server 133.100.11.8 prefer
server 210.72.145.44
server 203.117.180.36 //程序中所用的
server 131.107.1.10
server time.asia.apple.com
server 64.236.96.53
server 130.149.17.21
server 66.92.68.246
server www.freebsd.org
server 18.145.0.30
server clock.via.net
server 137.92.140.80
server 133.100.9.2
server 128.118.46.3
server ntp.nasa.gov
server 129.7.1.66
server ntp-sop.inria.frserver 210.72.145.44(国家授时中心服务器IP地址)
ntpdate 131.107.1.10
ntpdate -s time.asia.apple.com
try
URL url = new URL("http://www.bjtime.cn");// 取得资源对象
URLConnection uc = url.openConnection();// 生成连接对象
uc.connect(); // 发出连接
long ld = uc.getDate(); // 取得网站日期时间
Date date = new Date(ld); // 转换为标准时间对象
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int day = calendar.get(Calendar.DAY_OF_MONTH);
if (day == 6)
return true;
catch (Exception e)
e.printStackTrace();
参考技术B 中国科学院国家授时中心
string webUrl = http://www.ntsc.ac.cn
/**
* 获取指定网站的日期时间
*
* @param webUrl
* @return
* @author SHANHY
* @date 2015年11月27日
*/
private static String getWebsiteDatetime(String webUrl)
try
URL url = new URL(webUrl);// 取得资源对象
URLConnection uc = url.openConnection();// 生成连接对象
uc.connect();// 发出连接
long ld = uc.getDate();// 读取网站日期时间
Date date = new Date(ld);// 转换为标准时间对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);// 输出北京时间
return sdf.format(date);
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
return null;
参考技术C 是在网页里写吗???追问
不是,用java代码获取的
java如何获取当前登录ip
为适应不同的网络情况,提供这个类:看下面的代码吧,Copy过去就能用。import javax.servlet.http.HttpServletRequest;
public class HttpUtil
public static String getIpAddr(HttpServletRequest request)
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("Proxy-Client-IP");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("WL-Proxy-Client-IP");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
ip = request.getRemoteAddr();
return ip;
参考技术A 第一种:获取本机的IP
Enumeration<NetworkInterface> netInterfaces = null;
try
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements())
NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements())
System.out.println("IP:" + ips.nextElement().getHostAddress());
ipTemp= ni.getInetAddresses().nextElement().getHostAddress();
if(ipTemp!="127.0.0.1" && !"127.0.0.1".equals(ipTemp))
ip=ipTemp;
catch(Exception ee)
ee.printStackTrace();
第二种:也是本机的:
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString();//获得本机IP 参考技术B request.getRemoteAddr();本回答被提问者采纳
以上是关于java获取网络当前时间的主要内容,如果未能解决你的问题,请参考以下文章
java 我获得单位为毫秒的当前时间,如何转化成年月日小时分格式?