Getting Date in HTTP format in Java

java 8 date format
java date format
string to date java 8
simpledateformat java 8
java get current date
http date format
simpledateformat parse
rfc 1123 date format

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

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);
    return dateFormat.format(calendar.getTime());

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

Parsing and formatting HTTP dates as used in cookies and other , RFC date format : Date Format « Data Type « Java Tutorial. RFC1123_PATTERN = "EEE, dd MMM yyyyy HH:mm:ss z"; /** * Format for http response header date field Format current date and time with the SimpleDateFormat: dd/MM/yyyy. 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



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

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

RFC date format : Date Format « Data Type « Java Tutorial, This tutorial explains how to parse and format dates in Java using the SimpleDateFormat class and the DateTimeFormatter class. In order to format dates using SimpleDateFormat, we first needs to define a String date format e.g. "dd-MM-yyyy" will print dates in that format e.g. 01-11-2012. You can defined format based upon identifiers supported by SimpleDateFormat class. e.g. d means day of month, y means year and M means Month of year.

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'")


RFC1123_DATE_TIME_FORMATTER.print(new DateTime())

Parsing and Formatting Dates in Java, The String returned by the format method contains the formatted date and time that are to be displayed. Date today; String output; SimpleDateFormat formatter;  String pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat.format(new Date()); System.out.println(date); Output: 01-02-2018 In the example above, the date is 2nd January 2018.

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:


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");

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

Java SimpleDateFormat, It also allowed the formatting and parsing of date strings. Unfortunately http://​ As of JDK version 1.1, replaced by Calendar.get(Calendar. In Java, the SimpleDateFormat is usually used to format a date. This class is subclass of DateFormat. Its format method converts a date to a string. On the other hand, its parse method converts a string to a date.

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.

Customizing Formats (The Java™ Tutorials > Internationalization , @Override protected DateFormat initialValue() { // Date format specified by RFC 7231 section How can I get the current date and time in UTC or GMT in Java? See this. Another way to format dates is to use the DateTimeFormatter which works with the newer date time classes added in Java 8. Here is a DateTimeFormatter example of formatting a date as a string: DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE; String formattedDate = formatter.format ( ()); System.out.println

Date (Java Platform SE 7 ), Gets the date/time formatter with the default formatting style for the default FORMAT locale. static final DateFormat · getInstance(). Get a default  Get Current Date and Time: java.time.format.DateTimeFormatter The method returns the instance of LocalDateTime class. If we print the instance of LocalDateTime class, it prints current date and time. To format the current date, you can use DateTimeFormatter class which is included in JDK 1.8.

java.text.DateFormat.setTimeZone java code examples, This is a very easy method to get current date and time in Java. SimpleDateFormat is a concrete class for formatting and parsing dates in a locale​-sensitive  Display Current Date in Java; SimpleDateFormat: Parse and Format Dates; Compare Dates Example; Let us first understand the parameters that consist of a Date. It will primarily contain - The year (in either 2 or 4 digits) The month (in either 2 digits, First 3 letters of the month or the entire word of the month).

DateFormat, SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

  • @BasilBourque it does not comply if you consider the first 9 days of the month. Rfc 1123 supports 1-2 digits, but http 1.1 requires it to be two digits for the day-of-month.
  • @jontejj Thanks For the clarification. I deleted my comments as they were not helpful.
  • That code is a prime example of why people hate Java. I went with the answer by @jontejj.
  • It should be noted that use of SimpleDateFormat is not thread-safe and dateFormat must be non-shared or properly synchronized if shared. Better go with Java 8 or Joda Time classes.
  • This was a good answer in 2011. It’s just wearing… :-) Certainly agree to look further down the answer list now.
  • This Answer is now outdated, using troublesome old date-time classes that were supplanted by the java.time classes built into Java 8 and later.
  • Lovely to see this in Java 8 but, this didn't quite work for me I had to use instead of
  • I used this except with"GMT")). Thanks for the answer.
  • This works only for dates on or after 10th. For single digit dates, RFC 5322 expects the date to be of two digits, but this line of code generates a single digit date field.
  • @PavanKumar: Good point, but I think the reference is wrong :) RFC 5322, section 3.3 says day = ([FWS] 1*2DIGIT FWS) / obs-day. 1*2DIGIT means "at least one, at most two of DIGIT" ( as referenced by RFC 5322). For the purpose of HTTP/1.1 though: RFC 2616, section 3.3., references a "fixed-length" format of RFC 1123 (which uses the same definition 1*2DIGIT), defining day as 2DIGIT. It's a mess :)
  • @jontejj You could replace that ZoneId.of("GMT") with the predefined constant ZoneOffset.UTC.
  • This worked great. However, to ensure an English locale I had to add: private static final DateTimeFormatter RFC1123_DATE_TIME_FORMATTER = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'").withZoneUTC().withLocale(Locale.US);