package com.reg.web;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 作者:BealHe
* @date 时间:2018年5月9日 上午9:57:41
* @explain 类说明:抓取网络时间测试
*/
public class Test {
public static void main(String[] args) throws Exception {
// new一个我们需要抓取时间的网址,此处我写的是百度
URL url = new URL("https://www.baidu.com");
// 连接到我们需要抓取时间的网址
URLConnection uc = url.openConnection();
uc.connect();
// 抓取百度上的时间,获取到的时间为long类型
long time = uc.getDate();
// 将时间进行转型
Date d = new Date(time);
String date = new SimpleDateFormat("yyyy/MM/dd/ HH:mm:ss").format(d);
// 输出抓取到的时间
System.out.println("抓取到的百度网站时间为:"+date);
}
}
平常我们在开发的时候,很多时候不能使用本地系统时间,而是需要与网络时间同步。在这种情况下获取系统时间是不能满足我们的要求的,因此我在这儿写了这个博客,一遍以后自己使用,一方便广大同仁参考以及指教……