获取网络同步时间

来源:互联网 发布:怎么查看自己淘宝店铺 编辑:程序博客网 时间:2024/05/20 06:54
public static void main(String[] args) throws Exception {
      URL url=new URL("http://www.bjtime.cn");//取得资源对象
      URLConnection uc=url.openConnection();//生成连接对象
      uc.connect(); //发出连接
      long ld=uc.getDate(); //取得网站日期时间(时间戳)
      Date date=new Date(ld); //转换为标准时间对象
      System.out.print(date.getYear()+1900 + "年" +date.getMonth() + "月" + date.getDay() + "日" + 
      date.getHours() + "时" +date.getMinutes() +"分" + date.getSeconds() +"秒");

}


public static void main(String[] args) throws Exception {
URL url=new URL("http://www.bjtime.cn");//取得资源对象
URLConnection uc=url.openConnection();//生成连接对象
uc.connect(); //发出连接
long ld=uc.getDate(); //取得网站日期时间(时间戳)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日 hh 时 mm 分 ss 秒"); //设置格式化时间格式
String date = sdf.format(new Date(ld)); // 格式化时间
System.out.print(date);
}

1 0
原创粉丝点击