Getting Date in HTTP format in Java

2019-01-11 09:25发布

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/?

8条回答
Root(大扎)
2楼-- · 2019-01-11 09:32

java.time

If you're using Java 8 and later you can the predefined formatter for RFC 1123, DateTimeFormatter.RFC_1123_DATE_TIME.

java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC))
查看更多
闹够了就滚
3楼-- · 2019-01-11 09:33

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.

查看更多
Anthone
4楼-- · 2019-01-11 09:36
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
System.out.println("Date: " + dateFormat.format(calendar.getTime()));

You can play with it. The documentation is here: http://download.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

查看更多
5楼-- · 2019-01-11 09:41

Two-digit day-of-month

Some applications require the RFC1123 format to include a two digit day-of-month. The Java 8 DateTimeFormatter.RFC_1123_DATE_TIME uses a single digit:

System.out.println(DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneOffset.UTC)));

Output: Wed, 1 Aug 2018 14:56:46 GMT

Some applications don't like that. Before you use the old answers that use Joda-time or a pre-java8 SimpleDateFormat, here's a working Java-8 DateTimeFormatter:

DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss O")

Now, when you do this:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss O");
System.out.println(formatter.format(ZonedDateTime.now(ZoneOffset.UTC)));

You get Wed, 01 Aug 2018 14:56:46 GMT - note the leading zero in the day-of-month field.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-11 09:53

If you, like me, are trying to format a Java 8 java.time.Instant you need to explicitly add the time zone to the formatter. Like this:

Instant instant = Instant.now();
String formatted = DateTimeFormatter.RFC_1123_DATE_TIME
        .withZone(ZoneOffset.UTC)
        .format(instant);
System.out.println(formatted);

Which prints:

Tue, 15 Mar 2016 14:45:34 GMT

查看更多
Viruses.
7楼-- · 2019-01-11 09:54

If you are not afraid of additional dependencies, you can use apache DateUtils:

import org.apache.http.impl.cookie.DateUtils;
DateUtils.formatDate(new Date(System.currentTimeMillis()));
// Tue, 17 Apr 2012 18:59:02 GMT

This will format your date with respect to RFC 822 RFC1123.

查看更多
登录 后发表回答