Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Date in HTTP format in Java

Tags:

java

date

http

time

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

like image 691
Fergusmac Avatar asked Oct 10 '11 00:10

Fergusmac


People also ask

How to get Date in specific format in Java?

Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.

How to set Date in yyyy MM dd format in Java?

Formatting DatesString pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);

How to get Date from SimpleDateFormat in Java?

Creating A Simple Date Format A SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. String pattern = "yyyy-MM-dd" ; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The specified parameter “pattern” is the pattern used for formatting and parsing dates.


4 Answers

java.time

EDIT:

DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).withZone(ZoneId.of("GMT"))

is the way to do it with pure java.time. HTTP 1.1 is to not a 100% match with RFC 1123, so using the java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME formatter will fail for day-of-month less than 10. (thanks to @PavanKamar and @ankon for pointing that out)

Note: to be backwards compliant, you would need to also support the other two formats specified by RFC 2616

like image 130
jontejj Avatar answered Oct 17 '22 23:10

jontejj


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.

like image 21
Hannes R. Avatar answered Oct 17 '22 23:10

Hannes R.


If you're using Joda-Time (which I would highly recommend for any handling of dates and times in Java), you can do:

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

...

private static final DateTimeFormatter RFC1123_DATE_TIME_FORMATTER = 
    DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'")
    .withZoneUTC().withLocale(Locale.US);

...

RFC1123_DATE_TIME_FORMATTER.print(new DateTime())
like image 27
Mark Slater Avatar answered Oct 17 '22 22:10

Mark Slater


Two-digit day-of-month

Some applications require the format to include a two digit day-of-month as per RFC7231. 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.

like image 17
Malt Avatar answered Oct 17 '22 23:10

Malt