HTTP Last-Modified header contains date in following format (example):
Wed, 09 Apr 2008 23:55:38 GMT
What is the easiest way to parse java.util.Date from this string?
views:
747answers:
3
+5
A:
This should be pretty close
String dateString = "Wed, 09 Apr 2008 23:55:38 GMT";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
Date d = format.parse(dateString);
Shaun
2009-12-18 19:22:36
+1 almost correct, the `hh` should be `HH`, as the hours are 0-23.
notnoop
2009-12-18 19:24:54
Good catch, fixed now
Shaun
2009-12-18 19:26:37
I also had the timezone "ZZZ" instead of "zzz". Hopefully that will do it. If you still have issues you can reference the documentation linked above.
Shaun
2009-12-18 19:59:10
Works great for me, thank you! The only thing that I should say is that this example is for Locale.ENGLISH.
levanovd
2009-12-18 19:59:25
If you're doing this often make sure you reuse the SimpleDateFormat object (they're amazingly expensive to construct) and synchronize on it when calling `parse` (they're not threadsafe).
Ry4an
2009-12-18 20:08:52
The standard allows not one format, but **three** formats. http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
Sridhar Ratnakumar
2010-01-20 21:34:50
+4
A:
DateUtil.parseDate(dateString) (from apache commons-httpclient)
It has the correct format defined as a Constant, which is guarnateed to be compliant with the protocol.
Bozho
2009-12-18 19:32:30
+1
A:
RFC 2616 defines three different date formats that a conforming client must understand.
The Apache HttpClient provides a DateUtil that complies with the standard: http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/impl/cookie/DateUtils.html
Date date = DateUtil.parseDate( headerValue );
ralfstx
2010-01-09 17:47:50