java方式获取网络时间(国家标准时间)

转:http://blog.csdn.net/catoop/article/details/50076879
————————————————————————————————————————————

因为经测试发现http://www.bjtime.cn无法访问,于是对原代码略作修改,以供目前项目所用

/**
 * 
 */
package com.sydecm.util;


import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 读取网络时间
 *
 * @author SHANHY(365384722@QQ.COM)
 * @date   2015年11月27日
 */
public class GetNetworkTime {

//  public static String webUrl1 = "http://www.bjtime.cn";//bjTime
//  public static String webUrl2 = "http://www.baidu.com";//百度
//  public static String webUrl3 = "http://www.taobao.com";//淘宝
//  public static String webUrl4 = "http://www.ntsc.ac.cn";//中国科学院国家授时中心
//  public static String webUrl5 = "http://www.360.cn";//360
//  public static String webUrl6 = "http://www.beijing-time.org";//beijing-time

    public static List<String> webUrlList = new ArrayList<>();
    static{
        webUrlList.add("http://www.bjtime.cn");
        webUrlList.add("http://www.baidu.com");
        webUrlList.add("http://www.taobao.com");
        webUrlList.add("http://www.ntsc.ac.cn");
        webUrlList.add("http://www.360.cn");
        webUrlList.add("http://www.beijing-time.org");
    }
    public static void main(String[] args) {
        System.out.println(getWebsiteDatetime(webUrlList.get(0)) + " [bjtime]");
        System.out.println(getWebsiteDatetime(webUrlList.get(1)) + " [百度]");
        System.out.println(getWebsiteDatetime(webUrlList.get(2)) + " [淘宝]");
        System.out.println(getWebsiteDatetime(webUrlList.get(3)) + " [中国科学院国家授时中心]");
        System.out.println(getWebsiteDatetime(webUrlList.get(4)) + " [360安全卫士]");
        System.out.println(getWebsiteDatetime(webUrlList.get(5)) + " [beijing-time]");
        System.out.println(getWebsiteDatetimeActiveTimeStamp() + " [TimeStamp]");
        System.out.println(getWebsiteDatetimeActiveString() + " [String]");
        System.out.println(getWebsiteDatetimeActiveLong() + " [Long]");
        System.out.println(getWebsiteDatetimeActiveDate() + " [Date]");
    }

    /**
     * 获取指定网站的日期时间
     * 
     * @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;
    }

    /**
     * 获取指定网站的日期时间
     * 
     * @param webUrl
     * @return  返回日期字符串
     * @author HWJ(本人:372170989@QQ.COM,欢迎同行来一起交流努力)
     * @date   2017年7月7日
     */
    public static String getWebsiteDatetimeActiveString(){
        for (int i = 0; i < webUrlList.size(); i++) {
            try {
                URL url = new URL(webUrlList.get(i));// 取得资源对象
                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;
    }

    /**
     * 获取指定网站的日期时间
     * 
     * @param webUrl
     * @return  返回java.util.Date;
     * @author HWJ
     * @date   2017年7月7日
     */
    public static Date getWebsiteDatetimeActiveDate(){
        for (int i = 0; i < webUrlList.size(); i++) {
            try {
                URL url = new URL(webUrlList.get(i));// 取得资源对象
                URLConnection uc = url.openConnection();// 生成连接对象
                uc.connect();// 发出连接
                long ld = uc.getDate();// 读取网站日期时间
                Date date = new Date(ld);// 转换为标准时间对象
                return date;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 获取指定网站的日期时间
     * 
     * @param webUrl
     * @return  返回日期long类型
     * @author HWJ
     * @date   2017年7月7日
     */
    public static long getWebsiteDatetimeActiveLong(){
        for (int i = 0; i < webUrlList.size(); i++) {
            try {
                URL url = new URL(webUrlList.get(i));// 取得资源对象
                URLConnection uc = url.openConnection();// 生成连接对象
                uc.connect();// 发出连接
                long ld = uc.getDate();// 读取网站日期时间
                return ld;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }


    /**
     * 获取指定网站的日期时间
     * 
     * @param webUrl
     * @return  返回日期Timestamp类型
     * @author HWJ
     * @date   2017年7月7日
     */
    public static Timestamp getWebsiteDatetimeActiveTimeStamp(){
        for (int i = 0; i < webUrlList.size(); i++) {
            try {
                URL url = new URL(webUrlList.get(i));// 取得资源对象
                URLConnection uc = url.openConnection();// 生成连接对象
                uc.connect();// 发出连接
                long ld = uc.getDate();// 读取网站日期时间
                Timestamp ts = new Timestamp(ld);
                return ts;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值