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