View Javadoc

1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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 HTTP dates as used in cookies and other headers.  
46   * This class handles dates as defined by RFC 2616 section 3.3.1 as well as 
47   * some other common non-standard formats.
48   * 
49   * @author Christopher Brown
50   * @author Michael Becke
51   * 
52   * @deprecated Use {@link org.apache.commons.httpclient.util.DateUtil}
53   */
54  public class DateParser {
55  
56      /*** Log object for this class. */
57      private static final Log LOG = LogFactory.getLog(DateParser.class);
58  
59      /***
60       * Date format pattern used to parse HTTP date headers in RFC 1123 format.
61       */
62      public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
63  
64      /***
65       * Date format pattern used to parse HTTP date headers in RFC 1036 format.
66       */
67      public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
68  
69      /***
70       * Date format pattern used to parse HTTP date headers in ANSI C 
71       * <code>asctime()</code> format.
72       */
73      public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
74  
75      private static final Collection DEFAULT_PATTERNS = Arrays.asList(
76      		new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } );
77      /***
78       * Parses a date value.  The formats used for parsing the date value are retrieved from
79       * the default http params.
80       *
81       * @param dateValue the date value to parse
82       * 
83       * @return the parsed date
84       *
85       * @throws DateParseException if the value could not be parsed using any of the 
86       * supported date formats
87       */
88      public static Date parseDate(String dateValue) throws DateParseException {
89          return parseDate(dateValue, null);
90      }
91      
92      /***
93       * Parses the date value using the given date formats.
94       * 
95       * @param dateValue the date value to parse
96       * @param dateFormats the date formats to use
97       * 
98       * @return the parsed date
99       * 
100      * @throws DateParseException if none of the dataFormats could parse the dateValue
101      */
102     public static Date parseDate(
103         String dateValue, 
104         Collection dateFormats
105     ) throws DateParseException {
106         
107         if (dateValue == null) {
108             throw new IllegalArgumentException("dateValue is null");
109         }
110         if (dateFormats == null) {
111         	dateFormats = DEFAULT_PATTERNS;
112         }
113         // trim single quotes around date if present
114         // see issue #5279
115         if (dateValue.length() > 1 
116             && dateValue.startsWith("'") 
117             && dateValue.endsWith("'")
118         ) {
119             dateValue = dateValue.substring (1, dateValue.length() - 1);
120         }
121         
122         SimpleDateFormat dateParser = null;        
123         Iterator formatIter = dateFormats.iterator();
124         
125         while (formatIter.hasNext()) {
126             String format = (String) formatIter.next();            
127             if (dateParser == null) {
128                 dateParser = new SimpleDateFormat(format, Locale.US);
129                 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
130             } else {
131                 dateParser.applyPattern(format);                    
132             }
133             try {
134                 return dateParser.parse(dateValue);
135             } catch (ParseException pe) {
136                 // ignore this exception, we will try the next format
137             }                
138         }
139         
140         // we were unable to parse the date
141         throw new DateParseException("Unable to parse the date " + dateValue);        
142     }
143 
144     /*** This class should not be instantiated. */    
145     private DateParser() { }
146     
147 }