You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Chris Brown <br...@reflexe.fr> on 2003/04/11 15:44:43 UTC

Parsing "Last-Modified" HTTP response header

Hello,

Is there any built-in way in the Http Client API to obtain the value of the
"Last-Modified" header as a number of milliseconds or a Date (already
parsed), just like java.net.URLConnection.getLastModified() ?

Surely there's a method, or someone's already done that... Or do I have to
figure it out myself?! ;-)

Thanks,
Chris



Re: Parsing "Last-Modified" HTTP response header

Posted by Chris Brown <br...@reflexe.fr>.
Well, I'm an impatient chap... didn't find the code, so I coded it myself.

Here as a reference for anyone else (maybe worth including in the API, once
it's been verified).  Careful with line breaks...
____________________________________________________________

 /**
  * Date format pattern used to parse HTTP date headers in RFC 1123 format.
  * <p>Dates may be formatted using the following patterns (listed in order
  * of preference):</p>
  * <ol>
  * <li><code>RFC 1123 format</code> (recommended)</li>
  * <li>{@link #PATTERN_RFC1036 RFC 1036 format) (deprecated)</li>
  * <li>{@link #PATTERN_ASCTIME ANSI C asctime() format} (deprecated)</li>
  * </ol>
  */
 public static final String PATTERN_RFC1123 =
 "EEE, dd MMM yyyy HH:mm:ss zzz";

 /**
  * Date format pattern used to parse HTTP date headers in RFC 1036 format.
  * <p>Dates may be formatted using the following patterns (listed in order
  * of preference):</p>
  * <ol>
  * <li>{@link #PATTERN_RFC1123 RFC 1123 format} (recommended)</li>
  * <li><code>RFC 1036 format</code> (deprecated)</li>
  * <li>{@link #PATTERN_ASCTIME ANSI C asctime() format} (deprecated)</li>
  * </ol>
  */
 public static final String PATTERN_RFC1036 =
 "EEEE, dd-MMM-yy HH:mm:ss zzz";

 /**
  * Date format pattern used to parse HTTP date headers in ANSI C
<code>asctime()</code> format.
  * <p>Dates may be formatted using the following patterns (listed in order
  * of preference):</p>
  * <ol>
  * <li>{@link #PATTERN_RFC1123 RFC 1123 format} (recommended)</li>
  * <li>{@link #PATTERN_RFC1036 RFC 1036 format) (deprecated)</li>
  * <li><code>ANSI C asctime() format</code> (deprecated)</li>
  * </ol>
  */
 public static final String PATTERN_ASCTIME =
 "EEE MMM d HH:mm:ss yyyy";

 /**
  * Parses a HTTP header date value.
  *
  * @param dateHeaderValue the date header value to parse.
  * @return the parsed date.
  *
  * @throws ParseException if the date could not be parsed according to the
  *  one of the following formats (in order of preference):
  *  {@link #PATTERN_RFC1123 RFC 1123},
  *  {@link #PATTERN_RFC1036 RFC 1036},
  *  {@link #PATTERN_ASCTIME ANSI C asctime() format}.
  *
  *  @author Christopher Brown
  */
 public static Date parseDateHeader(String dateHeaderValue) throws
ParseException
 {
  Date date = null;
  if (dateHeaderValue != null)
  {
   SimpleDateFormat dateParser = new SimpleDateFormat(PATTERN_RFC1123,
Locale.US);
   dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
   try
   {
    date = dateParser.parse(dateHeaderValue);
   }
   catch (ParseException exRFC1123)
   {
    dateParser.applyPattern(PATTERN_RFC1036);
    try
    {
     date = dateParser.parse(dateHeaderValue);
    }
    catch (ParseException exRFC1036)
    {
     dateParser.applyPattern(PATTERN_ASCTIME);
     date = dateParser.parse(dateHeaderValue);
    }
   }
  }
  return date;
 }