Java 范例 - 获取 URL 响应头的日期信息
使用 HttpURLConnection 的 httpCon.getDate() 方法来获取 URL 响应头的日期信息
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.twle.cn"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); long date = httpCon.getDate(); if (date == 0) System.out.println("无法获取信息。"); else System.out.println("Date: " + new Date(date)); } }
编译运行以上 Java 代码,输出结果如下
Date: Mon May 04 11:57:06 CST 2015