Fergusmac Fergusmac - 3 months ago 20
HTTP Question

Getting Date in HTTP format in Java

I'm trying to get a String of a date in Java in the format specified in HTTP 1.1. Which, as far as I can tell, is:


Fri, 31 Dec 1999 23:59:59 GMT


With the time always being GMT.

What would be the easiest way to get this from Date/Calendar/?

Answer

In case someone else will try to find the answer here (like I did) here's what will do the trick:

String getServerTime() {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat(
        "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    return dateFormat.format(calendar.getTime());
}

in order to set the server to speak English and give time in GMT timezone.

Comments