Parse Expires HTTP Header
Hello,
I'm writing a HTTP-Proxyserver and I want to compare the cached Expires-Header with the actual date, so I need to convert the Header Object which I get from "httpclient" (jakarta) to a date Object. I tried the following:
/** Expires Header format. */
privatefinalstatic DateFormat expiresFormat =new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");
/** Expires header as a date. */
private Date expires;
...
Header expires = method.getResponseHeader("Expires");
// Parse the expires header.
try
{
this.expires = expiresFormat.parse(expires.toString());
}
catch (ParseException e)
{
logger.fatal("Bad date format in header: "+expires);
thrownew IllegalArgumentException(
"Bad date format in header: "+expires);
}
...
I get an Exception "Exception in thread "Thread-1" java.lang.IllegalArgumentException: Bad date format in header: Expires: Wed, 01 Jan 2020 01:01:01 GMT and I really don't know why :-/
I've tried so many "formats", but hmm, nothing worked so far :-/
greetings,
Johannes
[1750 byte] By [
Johannesa] at [2007-11-27 8:33:55]

# 1
Because the string you are trying to parse appears (from your exception output) to include the "Expires: " header string. DateFormat won't match a partial string AFAIR - it matches the whole string or nothing. Maybe try putting a literal 'Expires: ' at the from of your pattern, just like the 'GMT' at the end.
BTW, have you considered the possibility that the date will arrive with a different timezone on it?
# 2
Hm, same error, I have tried
'Expires:' EEE, dd MMM yyyy HH:mm:ss 'GMT'
and
'Expires: 'EEE, dd MMM yyyy HH:mm:ss 'GMT'
(first one with the whitespace not in the literal and second one within)
Do I also have to escape the comma? But it doesn't work either way ... I'm really getting crazy with this thing :-/
And yes I already thought about the time zone problem, maybe I should replace 'GMT' with zzz!?
# 4
Yes, but oh well, it doesn't work. Now I even tried
/** Expires Header format. */
private final static DateFormat expiresFormat = new SimpleDateFormat(
"'Wed, 01 Jan 2020 01:01:01 GMT'");
and it seems to work, at least there's no exception, but with logger.debug(this.expires) I can see that the date which is generated is "Thu Jan 01 00:00:00 CET 1970"? I'm really getting crazy, I would appreciate any help regarding why this date is generated and how I can really parse the expires date...
# 5
Well, this is given as a specific example in the SimpleDateFormat JavaDoc, and apart from them using z to capture the timezone, this looks good.
Can you show the actual exception 'e'? It's rarely good to dump the exception and replace it. Perhaps it has more information in it about what is going wrong? Just do 'e.printStackTrace();' inside the catch block (for now)
# 6
> Yes, but oh well, it doesn't work. Now I even tried
>
> /** Expires Header format. */
> private final static DateFormat expiresFormat = new
> SimpleDateFormat(
>"'Wed, 01 Jan 2020 01:01:01 GMT'");
>
That is illogical.
SimpleDateFormat takes a pattern. That isn't a pattern.
Presumably you intend to escape the entire thing perhaps? But then what exactly is it supposed to be parsing then?
And there is at least one bug with escaping so there could be others.
My suggestion was to stop escaping everything by removing the elements that you were already escaping. That would leave you just with a regular pattern.