| 12345678910111213141516171819202122232425262728293031323334 |
- package testExport;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- public class MyDate {
- private static Date getWebsiteDatetime(){
- String webUrl = "http://www.ntsc.ac.cn"; //中国科学院国家授时中心
- try {
- URL url = new URL(webUrl);// 取得资源对象
- 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;
- }
- public static void main(String[] args) {
- Date da = getWebsiteDatetime();
- System.out.println(da);
- }
- }
|