MyDate.java 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. package testExport;
  2. import java.io.IOException;
  3. import java.net.MalformedURLException;
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.Locale;
  9. public class MyDate {
  10. private static Date getWebsiteDatetime(){
  11. String webUrl = "http://www.ntsc.ac.cn"; //中国科学院国家授时中心
  12. try {
  13. URL url = new URL(webUrl);// 取得资源对象
  14. URLConnection uc = url.openConnection();// 生成连接对象
  15. uc.connect();// 发出连接
  16. long ld = uc.getDate();// 读取网站日期时间
  17. Date date = new Date(ld);// 转换为标准时间对象
  18. return date;
  19. } catch (MalformedURLException e) {
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. return null;
  25. }
  26. public static void main(String[] args) {
  27. Date da = getWebsiteDatetime();
  28. System.out.println(da);
  29. }
  30. }