1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java,v 1.3.2.1 2003/08/25 03:08:22 mbecke Exp $
3 * $Revision: 1.3.2.1 $
4 * $Date: 2003/08/25 03:08:22 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient.util;
65
66 import java.text.ParseException;
67 import java.text.SimpleDateFormat;
68 import java.util.Date;
69 import java.util.Locale;
70 import java.util.TimeZone;
71
72 /***
73 * A utility class for parsing HTTP dates as used in cookies and other headers.
74 * This class handles dates as defined by RFC 2616 section 3.3.1 as well as
75 * some other common non-standard formats.
76 *
77 * @author Christopher Brown
78 * @author Michael Becke
79 */
80 public class DateParser {
81
82 /***
83 * Date format pattern used to parse HTTP date headers in RFC 1123 format.
84 */
85 public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
86
87 /***
88 * Date format pattern used to parse HTTP date headers in RFC 1036 format.
89 */
90 public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
91
92 /***
93 * Date format pattern used to parse HTTP date headers in ANSI C
94 * <code>asctime()</code> format.
95 */
96 public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
97
98 /*** The patterns used for parsing dates */
99 private static final String[] DATE_PATTERNS = {
100 PATTERN_RFC1123,
101 PATTERN_RFC1036,
102 PATTERN_ASCTIME,
103 "EEE, dd-MMM-yyyy HH:mm:ss z",
104 "EEE, dd-MMM-yyyy HH-mm-ss z",
105 "EEE, dd MMM yy HH:mm:ss z",
106 "EEE dd-MMM-yyyy HH:mm:ss z",
107 "EEE dd MMM yyyy HH:mm:ss z",
108 "EEE dd-MMM-yyyy HH-mm-ss z",
109 "EEE dd-MMM-yy HH:mm:ss z",
110 "EEE dd MMM yy HH:mm:ss z",
111 "EEE,dd-MMM-yy HH:mm:ss z",
112 "EEE,dd-MMM-yyyy HH:mm:ss z",
113 "EEE, dd-MM-yyyy HH:mm:ss z",
114 };
115
116 /***
117 * Parses a date value.
118 *
119 * @param dateValue the date value to parse
120 *
121 * @return the parsed date
122 *
123 * @throws DateParseException if the value could not be parsed using any of the
124 * supported date formats
125 */
126 public static Date parseDate(String dateValue) throws DateParseException {
127 return parseDate(dateValue, DATE_PATTERNS);
128 }
129
130 /***
131 * Parses the date value using the array of date formats.
132 *
133 * @param dateValue the date value to parse
134 * @param dateFormats the date formats to use
135 *
136 * @return the parsed date
137 *
138 * @throws DateParseException if none of the dataFormats could parse the dateValue
139 */
140 private static Date parseDate(
141 String dateValue,
142 String[] dateFormats
143 ) throws DateParseException {
144
145 if (dateValue == null) {
146 throw new IllegalArgumentException("dateValue is null");
147 }
148
149 SimpleDateFormat dateParser = null;
150
151 for (int i = 0; i < dateFormats.length; i++) {
152 if (dateParser == null) {
153 dateParser = new SimpleDateFormat(dateFormats[i], Locale.US);
154 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
155 } else {
156 dateParser.applyPattern(dateFormats[i]);
157 }
158 try {
159 return dateParser.parse(dateValue);
160 } catch (ParseException pe) {
161 // ignore this exception, we will try the next format
162 }
163 }
164
165 // we were unable to parse the date
166 throw new DateParseException("Unable to parse the date " + dateValue);
167 }
168
169 /*** This class should not be instantiated. */
170 private DateParser() { }
171
172 }
This page was automatically generated by Maven