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 * @deprecated Use {@link org.apache.commons.httpclient.util.DateUtil}
53 */
54 public class DateParser {
55
56 /*** Log object for this class. */
57 private static final Log LOG = LogFactory.getLog(DateParser.class);
58
59 /***
60 * Date format pattern used to parse HTTP date headers in RFC 1123 format.
61 */
62 public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
63
64 /***
65 * Date format pattern used to parse HTTP date headers in RFC 1036 format.
66 */
67 public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
68
69 /***
70 * Date format pattern used to parse HTTP date headers in ANSI C
71 * <code>asctime()</code> format.
72 */
73 public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
74
75 private static final Collection DEFAULT_PATTERNS = Arrays.asList(
76 new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } );
77 /***
78 * Parses a date value. The formats used for parsing the date value are retrieved from
79 * the default http params.
80 *
81 * @param dateValue the date value to parse
82 *
83 * @return the parsed date
84 *
85 * @throws DateParseException if the value could not be parsed using any of the
86 * supported date formats
87 */
88 public static Date parseDate(String dateValue) throws DateParseException {
89 return parseDate(dateValue, null);
90 }
91
92 /***
93 * Parses the date value using the given date formats.
94 *
95 * @param dateValue the date value to parse
96 * @param dateFormats the date formats to use
97 *
98 * @return the parsed date
99 *
100 * @throws DateParseException if none of the dataFormats could parse the dateValue
101 */
102 public static Date parseDate(
103 String dateValue,
104 Collection dateFormats
105 ) throws DateParseException {
106
107 if (dateValue == null) {
108 throw new IllegalArgumentException("dateValue is null");
109 }
110 if (dateFormats == null) {
111 dateFormats = DEFAULT_PATTERNS;
112 }
113
114
115 if (dateValue.length() > 1
116 && dateValue.startsWith("'")
117 && dateValue.endsWith("'")
118 ) {
119 dateValue = dateValue.substring (1, dateValue.length() - 1);
120 }
121
122 SimpleDateFormat dateParser = null;
123 Iterator formatIter = dateFormats.iterator();
124
125 while (formatIter.hasNext()) {
126 String format = (String) formatIter.next();
127 if (dateParser == null) {
128 dateParser = new SimpleDateFormat(format, Locale.US);
129 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
130 } else {
131 dateParser.applyPattern(format);
132 }
133 try {
134 return dateParser.parse(dateValue);
135 } catch (ParseException pe) {
136
137 }
138 }
139
140
141 throw new DateParseException("Unable to parse the date " + dateValue);
142 }
143
144 /*** This class should not be instantiated. */
145 private DateParser() { }
146
147 }