永发信息网

java获取网络当前时间 时间不对

答案:3  悬赏:30  手机版
解决时间 2021-02-25 12:59
public static Date getInternetDate(){
URL url;
Date currDate = null;
try {
url = new URL("http://www.bjtime.cn");
//取得资源对象
URLConnection uc=url.openConnection();//生成连接对象
uc.connect(); //发出连接
long ld=uc.getDate(); //取得网站日期时间
currDate =new Date(ld);

} catch (Exception e) {

e.printStackTrace();
}
return currDate;
}
}
---- currDate: Wed Jul 29 01:21:18 CST 2015 实际北京时间应该是 礼拜四 (30号)上午11点多
最佳答案
www.bjtime.cn的服务器时间是加密的,请从其他网站获取。
全部回答
如果你要获取的是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.                  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
CST是美国时间啊大哥,就是说该连接提供的方法拿的是美国时间
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
偷星九月天贪狼为什么叛变? 仅仅因为他是恶
王菲演唱会为什么会有很多明星名人去看
手机冲电时显示 正在充电46% ,可1小时后看了
太害羞,不敢和女孩子搭话怎么办!
为什么打开Hyper-V之后,CPU频率被固定了
作文:如果可以,我很想_(字数不少于550字)
形容饿的成语是
世界有没有冥婚
一个国防科大的男生,爷爷是市级人大代表,爸
新中源瓷砖好还是箭牌瓷砖好?
三国群英传7怎么降伏麒麟有限制数目
急求、SHE的一首歌
万水千总是情,给我六佰行不行,下联
【学习周报】小学语文人教版六年级期末测评试
如何解除行政警告处分
推荐资讯
替身~~原来我只是他的替身~~
卫星天线天诚TCD-779,只能收到两三个台贵州
大东区华晨宝马邮编是多少
北京中路/北纬一路(路口)地址在什么地方,想
酥炸带鱼的做法,酥炸带鱼怎么做好吃,酥炸带
关于工程物资的盘亏或毁损,影响固定资产的账
吉利汽车空调调控按钮没有反应是什么原因
奥迪Q5,17年4月买的,进取版,价格392000元
【戏剧表演】电影表演和戏剧表演有什么不同?
判断函数f(x)=1?|x||x+2|?2
汽车,加装导航,破了原车线有事嘛!
应用宝下载的 彩票 可以买吗
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?