| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package com.happy.Until.Time;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Locale;
- public class OnlineTime {
- public static String webUrl4 = "http://www.ntsc.ac.cn";//中国科学院国家授时中心
- public 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);// 转换为标准时间对象
- System.out.println(date);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 输出北京时间
- return sdf.format(date);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static String getWebsiteDateday(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");// 输出北京时间
- return sdf.format(date);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- public static String time(){
- return getWebsiteDatetime(webUrl4);
- }
- public static String day(){
- return getWebsiteDateday(webUrl4);
- }
- public static void main(String[] args) {
- System.out.println(day());
- System.out.println(time());
- }
- }
|