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.Calendar;
36 import java.util.Collection;
37 import java.util.Date;
38 import java.util.Iterator;
39 import java.util.Locale;
40 import java.util.TimeZone;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 /***
46 * A utility class for parsing and formatting HTTP dates as used in cookies and
47 * other headers. This class handles dates as defined by RFC 2616 section
48 * 3.3.1 as well as some other common non-standard formats.
49 *
50 * @author Christopher Brown
51 * @author Michael Becke
52 */
53 public class DateUtil {
54
55 /*** Log object for this class. */
56 private static final Log LOG = LogFactory.getLog(DateUtil.class);
57
58 /***
59 * Date format pattern used to parse HTTP date headers in RFC 1123 format.
60 */
61 public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
62
63 /***
64 * Date format pattern used to parse HTTP date headers in RFC 1036 format.
65 */
66 public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
67
68 /***
69 * Date format pattern used to parse HTTP date headers in ANSI C
70 * <code>asctime()</code> format.
71 */
72 public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
73
74 private static final Collection DEFAULT_PATTERNS = Arrays.asList(
75 new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } );
76
77 private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
78
79 static {
80 Calendar calendar = Calendar.getInstance();
81 calendar.set(2000, Calendar.JANUARY, 1, 0, 0);
82 DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime();
83 }
84
85 /***
86 * Parses a date value. The formats used for parsing the date value are retrieved from
87 * the default http params.
88 *
89 * @param dateValue the date value to parse
90 *
91 * @return the parsed date
92 *
93 * @throws DateParseException if the value could not be parsed using any of the
94 * supported date formats
95 */
96 public static Date parseDate(String dateValue) throws DateParseException {
97 return parseDate(dateValue, null, null);
98 }
99
100 /***
101 * Parses the date value using the given date formats.
102 *
103 * @param dateValue the date value to parse
104 * @param dateFormats the date formats to use
105 *
106 * @return the parsed date
107 *
108 * @throws DateParseException if none of the dataFormats could parse the dateValue
109 */
110 public static Date parseDate(String dateValue, Collection dateFormats)
111 throws DateParseException {
112 return parseDate(dateValue, dateFormats, null);
113 }
114
115 /***
116 * Parses the date value using the given date formats.
117 *
118 * @param dateValue the date value to parse
119 * @param dateFormats the date formats to use
120 * @param startDate During parsing, two digit years will be placed in the range
121 * <code>startDate</code> to <code>startDate + 100 years</code>. This value may
122 * be <code>null</code>. When <code>null</code> is given as a parameter, year
123 * <code>2000</code> will be used.
124 *
125 * @return the parsed date
126 *
127 * @throws DateParseException if none of the dataFormats could parse the dateValue
128 */
129 public static Date parseDate(
130 String dateValue,
131 Collection dateFormats,
132 Date startDate
133 ) throws DateParseException {
134
135 if (dateValue == null) {
136 throw new IllegalArgumentException("dateValue is null");
137 }
138 if (dateFormats == null) {
139 dateFormats = DEFAULT_PATTERNS;
140 }
141 if (startDate == null) {
142 startDate = DEFAULT_TWO_DIGIT_YEAR_START;
143 }
144
145
146 if (dateValue.length() > 1
147 && dateValue.startsWith("'")
148 && dateValue.endsWith("'")
149 ) {
150 dateValue = dateValue.substring (1, dateValue.length() - 1);
151 }
152
153 SimpleDateFormat dateParser = null;
154 Iterator formatIter = dateFormats.iterator();
155
156 while (formatIter.hasNext()) {
157 String format = (String) formatIter.next();
158 if (dateParser == null) {
159 dateParser = new SimpleDateFormat(format, Locale.US);
160 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
161 dateParser.set2DigitYearStart(startDate);
162 } else {
163 dateParser.applyPattern(format);
164 }
165 try {
166 return dateParser.parse(dateValue);
167 } catch (ParseException pe) {
168
169 }
170 }
171
172
173 throw new DateParseException("Unable to parse the date " + dateValue);
174 }
175
176 /***
177 * Formats the given date according to the RFC 1123 pattern.
178 *
179 * @param date The date to format.
180 * @return An RFC 1123 formatted date string.
181 *
182 * @see #PATTERN_RFC1123
183 */
184 public static String formatDate(Date date) {
185 return formatDate(date, PATTERN_RFC1123);
186 }
187
188 /***
189 * Formats the given date according to the specified pattern. The pattern
190 * must conform to that used by the {@link SimpleDateFormat simple date
191 * format} class.
192 *
193 * @param date The date to format.
194 * @param pattern The pattern to use for formatting the date.
195 * @return A formatted date string.
196 *
197 * @throws IllegalArgumentException If the given date pattern is invalid.
198 *
199 * @see SimpleDateFormat
200 */
201 public static String formatDate(Date date, String pattern) {
202 if (date == null) throw new IllegalArgumentException("date is null");
203 if (pattern == null) throw new IllegalArgumentException("pattern is null");
204
205 SimpleDateFormat formatter = new SimpleDateFormat(pattern);
206 return formatter.format(date);
207 }
208
209 /*** This class should not be instantiated. */
210 private DateUtil() { }
211
212 }