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 and formatting HTTP dates as used in cookies and
46 * other headers. This class handles dates as defined by RFC 2616 section
47 * 3.3.1 as well as some other common non-standard formats.
48 *
49 * @author Christopher Brown
50 * @author Michael Becke
51 */
52 public class DateUtil {
53
54 /*** Log object for this class. */
55 private static final Log LOG = LogFactory.getLog(DateUtil.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 /***
77 * Parses a date value. The formats used for parsing the date value are retrieved from
78 * the default http params.
79 *
80 * @param dateValue the date value to parse
81 *
82 * @return the parsed date
83 *
84 * @throws DateParseException if the value could not be parsed using any of the
85 * supported date formats
86 */
87 public static Date parseDate(String dateValue) throws DateParseException {
88 return parseDate(dateValue, null);
89 }
90
91 /***
92 * Parses the date value using the given date formats.
93 *
94 * @param dateValue the date value to parse
95 * @param dateFormats the date formats to use
96 *
97 * @return the parsed date
98 *
99 * @throws DateParseException if none of the dataFormats could parse the dateValue
100 */
101 public static Date parseDate(
102 String dateValue,
103 Collection dateFormats
104 ) throws DateParseException {
105
106 if (dateValue == null) {
107 throw new IllegalArgumentException("dateValue is null");
108 }
109 if (dateFormats == null) {
110 dateFormats = DEFAULT_PATTERNS;
111 }
112
113
114 if (dateValue.length() > 1
115 && dateValue.startsWith("'")
116 && dateValue.endsWith("'")
117 ) {
118 dateValue = dateValue.substring (1, dateValue.length() - 1);
119 }
120
121 SimpleDateFormat dateParser = null;
122 Iterator formatIter = dateFormats.iterator();
123
124 while (formatIter.hasNext()) {
125 String format = (String) formatIter.next();
126 if (dateParser == null) {
127 dateParser = new SimpleDateFormat(format, Locale.US);
128 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
129 } else {
130 dateParser.applyPattern(format);
131 }
132 try {
133 return dateParser.parse(dateValue);
134 } catch (ParseException pe) {
135
136 }
137 }
138
139
140 throw new DateParseException("Unable to parse the date " + dateValue);
141 }
142
143 /***
144 * Formats the given date according to the RFC 1123 pattern.
145 *
146 * @param date The date to format.
147 * @return An RFC 1123 formatted date string.
148 *
149 * @see #PATTERN_RFC1123
150 */
151 public static String formatDate(Date date) {
152 return formatDate(date, PATTERN_RFC1123);
153 }
154
155 /***
156 * Formats the given date according to the specified pattern. The pattern
157 * must conform to that used by the {@link SimpleDateFormat simple date
158 * format} class.
159 *
160 * @param date The date to format.
161 * @param pattern The pattern to use for formatting the date.
162 * @return A formatted date string.
163 *
164 * @throws IllegalArgumentException If the given date pattern is invalid.
165 *
166 * @see SimpleDateFormat
167 */
168 public static String formatDate(Date date, String pattern) {
169 if (date == null) throw new IllegalArgumentException("date is null");
170 if (pattern == null) throw new IllegalArgumentException("pattern is null");
171
172 SimpleDateFormat formatter = new SimpleDateFormat(pattern);
173 return formatter.format(date);
174 }
175
176 /*** This class should not be instantiated. */
177 private DateUtil() { }
178
179 }