JAVA 获取网络时间

以你之姓@ 2022-08-20 06:09 347阅读 0赞
  1. package com.my.consist;
  2. /**
  3. * 日期常量接口
  4. *
  5. * @author wbw
  6. *
  7. */
  8. public interface DateConsist {
  9. public static final String WEB_URL1 = "http://www.bjtime.cn";// bjTime
  10. public static final String WEB_URL2 = "http://www.baidu.com";// 百度
  11. public static final String WEB_URL3 = "http://www.taobao.com";// 淘宝
  12. public static final String WEB_URL4 = "http://www.ntsc.ac.cn";// 中国科学院国家授时中心
  13. public static final String WEB_URL5 = "http://www.360.cn";// 360
  14. public static final String WEB_URL6 = "http://www.beijing-time.org";// beijing-time
  15. public static final String WEB_URL7 = "http://www.jd.com/";// 京东
  16. }
  17. package com.my.utils;
  18. import java.net.URL;
  19. import java.net.URLConnection;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. import java.util.Locale;
  23. import org.springframework.util.StringUtils;
  24. import com.my.consist.DateConsist;
  25. /**
  26. * 获取网络时间
  27. *
  28. * @author wbw
  29. *
  30. */
  31. public class WebDateUtils {
  32. /**
  33. * 默认时间格式
  34. */
  35. private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
  36. /**
  37. * 根据URL和格式化类型获取时间
  38. *
  39. * @param webuUrl
  40. * 网络URL
  41. * @param format
  42. * 格式
  43. * @return
  44. */
  45. public static String getWebsiteDatetime(String webuUrl, String format) {
  46. try {
  47. // 判断当前是否传入URL
  48. if (!StringUtils.isEmpty(webuUrl)) {
  49. URL url = new URL(webuUrl);// 获取url对象
  50. URLConnection uc = url.openConnection();// 获取生成连接对象
  51. uc.connect();// 发出连接请求
  52. long ld = uc.getDate();// 读取网站日期时间
  53. Date date = new Date(ld);// 转化为时间对象
  54. SimpleDateFormat sdf = new SimpleDateFormat(
  55. format != null ? format : DEFAULT_FORMAT, Locale.CHINA);// 输出北京时间
  56. return sdf.format(date);
  57. } else {
  58. System.out.println("URL Error!!!");
  59. }
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. return null;
  64. }
  65. /**
  66. * 测试
  67. *
  68. * @param args
  69. */
  70. public static void main(String[] args) {
  71. String format = "yyyy-MM-dd HH:mm:ss";
  72. System.out.println(getWebsiteDatetime(DateConsist.WEB_URL1, format)
  73. + " [bjtime]");
  74. System.out.println(getWebsiteDatetime(DateConsist.WEB_URL2, format)
  75. + " [百度]");
  76. System.out.println(getWebsiteDatetime(DateConsist.WEB_URL3, format)
  77. + " [淘宝]");
  78. System.out.println(getWebsiteDatetime(DateConsist.WEB_URL4, format)
  79. + " [中国科学院国家授时中心]");
  80. System.out.println(getWebsiteDatetime(DateConsist.WEB_URL5, format)
  81. + " [360安全卫士]");
  82. System.out.println(getWebsiteDatetime(DateConsist.WEB_URL6, format)
  83. + " [beijing-time]");
  84. System.out.println(getWebsiteDatetime(DateConsist.WEB_URL7, format)
  85. + " [京东-time]");
  86. }
  87. }

控制台输出信息

  1. 2016-02-01 16:28:27 [bjtime]
  2. 2016-02-03 16:27:47 [百度]
  3. 2016-02-03 16:27:47 [淘宝]
  4. 2016-02-03 16:27:47 [中国科学院国家授时中心]
  5. 2016-02-03 16:27:47 [360安全卫士]
  6. 2016-02-03 16:27:47 [beijing-time]
  7. 2016-02-03 16:29:25 [京东-time]

发表评论

表情:
评论列表 (有 0 条评论,347人围观)

还没有评论,来说两句吧...

相关阅读