2

I'm surprised to fail to find how to convert Java Date object to the string that is a valid HTTP If-Modified-Since date format.

I've found a question about data format that answers what format it is, but not how to produce that format.

The solution the most nearby I've found is new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");, however it is not working because HTTP Server accepts only GMT Time Zone.

The solution would be either to correctly add time zone information (without causing the time shift) or to applying time shift manually, based on current timezone information, and appending 'GMT' manually.

But I can hardly believe there's no utility function doing that. At least Apache HttpCommons should have such code. It's not rocket science. It's one of the basic functionality required to cooperate with Http Server.

Have I failed googling? What is the simplest reliable way to produce If-Modified-Since header?

9ilsdx 9rvj 0lo
  • 7,955
  • 10
  • 38
  • 77
  • Possible duplicate of [If-Modified-Since Date Format](https://stackoverflow.com/questions/13593796/if-modified-since-date-format) – Akirus Aug 08 '19 at 09:58

1 Answers1

1

You can use org.apache.http.impl.cookie.DateUtils.

The method to use is formatDate(Date date, String pattern) - String patterns allowed RFC 1036, RFC 1123 (Constants declared in the same class).

In case you need the dependency:

<dependency>
   <groupId>org.apache.httpcomponents</groupId>  
   <artifactId>httpclient</artifactId>
  <version>4.3.3</version>
</dependency>

Happy Coding!!

bidisha mukherjee
  • 715
  • 1
  • 10
  • 20
  • Thanks, it's exactly that one-liner. Thought now I've learned, the problem is not that server accepts only GMT, but it accepts only exactly the same time literal as returned in Last-Modified header. – 9ilsdx 9rvj 0lo Aug 08 '19 at 10:29