我们用 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.baidu.com");HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();long date = httpCon.getDate();if (date == 0)System.out.println("无法获取信息。");elseSystem.out.println("Date: " + new Date(date));}
}
结果输出
Date: Mon May 06 18:02:35 CST 2019