View Javadoc

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