001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java,v 1.11 2004/11/06 19:15:42 mbecke Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.util;
032:
033: import java.text.ParseException;
034: import java.text.SimpleDateFormat;
035: import java.util.Arrays;
036: import java.util.Collection;
037: import java.util.Date;
038: import java.util.Iterator;
039: import java.util.Locale;
040: import java.util.TimeZone;
041:
042: /**
043: * A utility class for parsing HTTP dates as used in cookies and other headers.
044: * This class handles dates as defined by RFC 2616 section 3.3.1 as well as
045: * some other common non-standard formats.
046: *
047: * @author Christopher Brown
048: * @author Michael Becke
049: *
050: * @deprecated Use {@link org.apache.commons.httpclient.util.DateUtil}
051: */
052: public class DateParser {
053:
054: /**
055: * Date format pattern used to parse HTTP date headers in RFC 1123 format.
056: */
057: public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
058:
059: /**
060: * Date format pattern used to parse HTTP date headers in RFC 1036 format.
061: */
062: public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
063:
064: /**
065: * Date format pattern used to parse HTTP date headers in ANSI C
066: * <code>asctime()</code> format.
067: */
068: public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
069:
070: private static final Collection DEFAULT_PATTERNS = Arrays
071: .asList(new String[] { PATTERN_ASCTIME, PATTERN_RFC1036,
072: PATTERN_RFC1123 });
073:
074: /**
075: * Parses a date value. The formats used for parsing the date value are retrieved from
076: * the default http params.
077: *
078: * @param dateValue the date value to parse
079: *
080: * @return the parsed date
081: *
082: * @throws DateParseException if the value could not be parsed using any of the
083: * supported date formats
084: */
085: public static Date parseDate(String dateValue)
086: throws DateParseException {
087: return parseDate(dateValue, null);
088: }
089:
090: /**
091: * Parses the date value using the given date formats.
092: *
093: * @param dateValue the date value to parse
094: * @param dateFormats the date formats to use
095: *
096: * @return the parsed date
097: *
098: * @throws DateParseException if none of the dataFormats could parse the dateValue
099: */
100: public static Date parseDate(String dateValue,
101: Collection dateFormats) throws DateParseException {
102:
103: if (dateValue == null) {
104: throw new IllegalArgumentException("dateValue is null");
105: }
106: if (dateFormats == null) {
107: dateFormats = DEFAULT_PATTERNS;
108: }
109: // trim single quotes around date if present
110: // see issue #5279
111: if (dateValue.length() > 1 && dateValue.startsWith("'")
112: && dateValue.endsWith("'")) {
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: // ignore this exception, we will try the next format
131: }
132: }
133:
134: // we were unable to parse the date
135: throw new DateParseException("Unable to parse the date "
136: + dateValue);
137: }
138:
139: /** This class should not be instantiated. */
140: private DateParser() {
141: }
142:
143: }
|