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?
相关问题
- Angular RxJS mergeMap types
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
RFC 2616 defines three different date formats that a conforming client must understand.
The Apache HttpClient provides a DateUtil that complies with the standard:
https://hc.apache.org/httpcomponents-client-4.3.x/httpclient/apidocs/org/apache/http/client/utils/DateUtils.html
https://apache.googlesource.com/httpclient/+/4.3.x/httpclient/src/main/java/org/apache/http/client/utils/DateUtils.java
Date date = DateUtils.parseDate( headerValue );
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.
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.java.time
When using the new Java Date and Time API the code would simply be:
The
DateTimeFormatter
class pre-defines a constant for that particular format inRFC_1123_DATE_TIME
. As the name suggests, RFC 1123 defines that format.This should be pretty close
SimpleDateFormat