JAVA 获取网络时间

               
package com.my.consist;/** * 日期常量接口 *  * @author wbw *  */public interface DateConsist public static final String WEB_URL1 = "http://www.bjtime.cn";// bjTime public static final String WEB_URL2 = "http://www.baidu.com";// 百度 public static final String WEB_URL3 = "http://www.taobao.com";// 淘宝 public static final String WEB_URL4 = "http://www.ntsc.ac.cn";// 中国科学院国家授时中心 public static final String WEB_URL5 = "http://www.360.cn";// 360 public static final String WEB_URL6 = "http://www.beijing-time.org";// beijing-time public static final String WEB_URL7 = "http://www.jd.com/";// 京东}

package com.my.utils;import java.net.URL;import java.net.URLConnection;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import org.springframework.util.StringUtils;import com.my.consist.DateConsist;/** * 获取网络时间 *  * @author wbw *  */public class WebDateUtils /**  * 默认时间格式  */ private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss"/**  * 根据URL和格式化类型获取时间  *   * @param webuUrl  *            网络URL  * @param format  *            格式  * @return  */ public static String getWebsiteDatetime(String webuUrl, String format) {  try {   // 判断当前是否传入URL   if (!StringUtils.isEmpty(webuUrl)) {    URL url = new URL(webuUrl);// 获取url对象    URLConnection uc = url.openConnection();// 获取生成连接对象    uc.connect();// 发出连接请求    long ld = uc.getDate();// 读取网站日期时间    Date date = new Date(ld);// 转化为时间对象    SimpleDateFormat sdf = new SimpleDateFormat(      format != null ? format : DEFAULT_FORMAT, Locale.CHINA);// 输出北京时间    return sdf.format(date);   } else {    System.out.println("URL Error!!!");   }  } catch (Exception e) {   e.printStackTrace();  }  return null; } /**  * 测试  *   * @param args  */ public static void main(String[] args) {  String format = "yyyy-MM-dd HH:mm:ss";  System.out.println(getWebsiteDatetime(DateConsist.WEB_URL1, format)    + " [bjtime]");  System.out.println(getWebsiteDatetime(DateConsist.WEB_URL2, format)    + " [百度]");  System.out.println(getWebsiteDatetime(DateConsist.WEB_URL3, format)    + " [淘宝]");  System.out.println(getWebsiteDatetime(DateConsist.WEB_URL4, format)    + " [中国科学院国家授时中心]");  System.out.println(getWebsiteDatetime(DateConsist.WEB_URL5, format)    + " [360安全卫士]");  System.out.println(getWebsiteDatetime(DateConsist.WEB_URL6, format)    + " [beijing-time]");  System.out.println(getWebsiteDatetime(DateConsist.WEB_URL7, format)    + " [京东-time]"); }}

控制台输出信息

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


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Java中的`java.net.URL`和`java.HttpURLConnection`类来获取网络时间。以下是一个示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class NetworkTimeExample { public static void main(String[] args) { try { // 创建URL对象 URL url = new URL("http://www.baidu.com"); // 打开URL连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方式为GET connection.setRequestMethod("GET"); // 获取响应码 int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // 读取响应内容 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 解析响应内容提取时间信息 String timeStr = response.toString(); // 假设响应内容包含时间信息 // 进行时间解析和处理 System.out.println("网络时间:" + timeStr); } else { System.out.println("获取网络时间失败,响应码:" + responseCode); } // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个例子通过发送一个GET请求到百度网址(http://www.baidu.com),然后获取到响应内容并解析出时间信息。您可以根据自己的需求修改URL地址和解析方式。注意,这种方法获取的是服务器的时间,而不是本地时间

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值