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?
60
votes
5 Answers
73
votes
63
votes
DateUtil.parseDate(dateString)
from apache http-components
(legacy: DateUtil.parseDate(dateString)
(from apache commons-httpclient))
It has the correct format defined as a Constant, which is guaranteed to be compliant with the protocol.
27
votes
java.time
When using the new Java Date and Time API the code would simply be:
ZonedDateTime zdt = ZonedDateTime.parse("Wed, 09 Apr 2008 23:55:38 GMT", DateTimeFormatter.RFC_1123_DATE_TIME);
The DateTimeFormatter
class pre-defines a constant for that particular format in RFC_1123_DATE_TIME
. As the name suggests, RFC 1123 defines that format.
24
votes
5
votes
If you're using URLConnection
s, there is already a handy method.
See URLConnection#getLastModified
This method parses the date string and returns a milliseconds value. Then you can happily create a Date
with that value.