■ サンプル
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.ibm.icu.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = getDatetimeFromResponseHeader("https://blogs.yahoo.co.jp/dk521123/37095462.html", 10_000);
System.out.println(toString(calendar));
}
public static Calendar getDatetimeFromResponseHeader(String url, int timeout) {
try {
URL targetUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setRequestMethod("HEAD");
Calendar returnValue = Calendar.getInstance();
returnValue.setTimeInMillis(connection.getDate());
return returnValue;
} catch (IOException exception) {
return null;
}
}
public static String toString(Calendar calendar){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");
return dateFormat.format(calendar.getTime());
}
}