Java 实例 - 获取 URL响应头的日期信息

Java 实例 Java 实例

以下实例演示了如何使用 HttpURLConnection 的 httpCon.getDate() 方法来获取 URL响应头的日期信息:

/*
 author by w3cschool.cn
 Main.java
 */

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class Main{
   public static void main(String args[]) 
   throws Exception {
      URL url = new URL("http://www.sxiaw.com");
      HttpURLConnection httpCon = 
      (HttpURLConnection) url.openConnection();
      long date = httpCon.getDate();
      if (date == 0)
      System.out.println("无法获取信息。");
      else
      System.out.println("Date: " + new Date(date));
   }
}

以上代码运行输出结果为:

Date: Mon May 04 11:57:06 CST 2015

Java 实例 Java 实例