1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient.util;
31
32 import java.text.ParseException;
33 import java.text.SimpleDateFormat;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Date;
37 import java.util.Iterator;
38 import java.util.Locale;
39 import java.util.TimeZone;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43
44 /***
45 * A utility class for parsing HTTP dates as used in cookies and other headers.
46 * This class handles dates as defined by RFC 2616 section 3.3.1 as well as
47 * some other common non-standard formats.
48 *
49 * @author Christopher Brown
50 * @author Michael Becke
51 */
52 public class DateParser {
53
54 /*** Log object for this class. */
55 private static final Log LOG = LogFactory.getLog(DateParser.class);
56
57 /***
58 * Date format pattern used to parse HTTP date headers in RFC 1123 format.
59 */
60 public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
61
62 /***
63 * Date format pattern used to parse HTTP date headers in RFC 1036 format.
64 */
65 public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
66
67 /***
68 * Date format pattern used to parse HTTP date headers in ANSI C
69 * <code>asctime()</code> format.
70 */
71 public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
72
73 private static final Collection DEFAULT_PATTERNS = Arrays.asList(
74 new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } );
75 /***
76 * Parses a date value. The formats used for parsing the date value are retrieved from
77 * the default http params.
78 *
79 * @param dateValue the date value to parse
80 *
81 * @return the parsed date
82 *
83 * @throws DateParseException if the value could not be parsed using any of the
84 * supported date formats
85 */
86 public static Date parseDate(String dateValue) throws DateParseException {
87 return parseDate(dateValue, null);
88 }
89
90 /***
91 * Parses the date value using the given date formats.
92 *
93 * @param dateValue the date value to parse
94 * @param dateFormats the date formats to use
95 *
96 * @return the parsed date
97 *
98 * @throws DateParseException if none of the dataFormats could parse the dateValue
99 */
100 public static Date parseDate(
101 String dateValue,
102 Collection dateFormats
103 ) throws DateParseException {
104
105 if (dateValue == null) {
106 throw new IllegalArgumentException("dateValue is null");
107 }
108 if (dateFormats == null) {
109 dateFormats = DEFAULT_PATTERNS;
110 }
111
112
113 if (dateValue.length() > 1
114 && dateValue.startsWith("'")
115 && dateValue.endsWith("'")
116 ) {
117 dateValue = dateValue.substring (1, dateValue.length() - 1);
118 }
119
120 SimpleDateFormat dateParser = null;
121 Iterator formatIter = dateFormats.iterator();
122
123 while (formatIter.hasNext()) {
124 String format = (String) formatIter.next();
125 if (dateParser == null) {
126 dateParser = new SimpleDateFormat(format, Locale.US);
127 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
128 } else {
129 dateParser.applyPattern(format);
130 }
131 try {
132 return dateParser.parse(dateValue);
133 } catch (ParseException pe) {
134
135 }
136 }
137
138
139 throw new DateParseException("Unable to parse the date " + dateValue);
140 }
141
142 /*** This class should not be instantiated. */
143 private DateParser() { }
144
145 }