java获取网络时间_java获取网络当前时间

展开全部

如果你要获取的是Internet时间,可以使用NTP服务。

NTP概念简介

Network Time Protocol(NTP)是62616964757a686964616fe4b893e5b19e31333332643164用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,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 

secondsSince1900 = (secondsSince1900 <

}

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的`URL`和`URLConnection`类来获取网络时间。具体实现可以参考以下代码: ```java import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Date; public class GetNetworkTime { public static void main(String[] args) { String netTime = getNetworkTime(); System.out.println(netTime); } public static String getNetworkTime() { try { URL url = new URL("http://www.baidu.com");//使用百度作为网络时间的请求地址 URLConnection conn = url.openConnection(); conn.connect(); long netTime = conn.getDate(); Date date = new Date(netTime); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); } catch (Exception e) { e.printStackTrace(); } return null; } } ``` 在JSP页面中使用上述代码可以这样实现: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.Date"%> <%@page import="java.net.URL"%> <%@page import="java.net.URLConnection"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>获取网络时间</title> </head> <body> <% String netTime = getNetworkTime(); out.println("当前网络时间:" + netTime); %> <% String getNetworkTime() { try { URL url = new URL("http://www.baidu.com");//使用百度作为网络时间的请求地址 URLConnection conn = url.openConnection(); conn.connect(); long netTime = conn.getDate(); Date date = new Date(netTime); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(date); } catch (Exception e) { e.printStackTrace(); } return null; } %> </body> </html> ``` 当然,上述代码只是最简单的实现方式,如果需要更精确的时间可以使用更加复杂的算法,比如NTP协议等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值