View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java,v 1.10 2004/09/14 20:11:32 olegk Exp $
3    * $Revision: 1.10 $
4    * $Date: 2004/09/14 20:11:32 $
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  public class DateParser {
53  
54      /*** Log object for this class. */
55      private static final Log LOG = LogFactory.getLog(DateParser.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       * Parses a date value.  The formats used for parsing the date value are retrieved from
77       * the default http params.
78       *
79       * @param dateValue the date value to parse
80       * 
81       * @return the parsed date
82       *
83       * @throws DateParseException if the value could not be parsed using any of the 
84       * supported date formats
85       */
86      public static Date parseDate(String dateValue) throws DateParseException {
87          return parseDate(dateValue, null);
88      }
89      
90      /***
91       * Parses the date value using the given date formats.
92       * 
93       * @param dateValue the date value to parse
94       * @param dateFormats the date formats to use
95       * 
96       * @return the parsed date
97       * 
98       * @throws DateParseException if none of the dataFormats could parse the dateValue
99       */
100     public static Date parseDate(
101         String dateValue, 
102         Collection dateFormats
103     ) throws DateParseException {
104         
105         if (dateValue == null) {
106             throw new IllegalArgumentException("dateValue is null");
107         }
108         if (dateFormats == null) {
109         	dateFormats = DEFAULT_PATTERNS;
110         }
111         // trim single quotes around date if present
112         // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279
113         if (dateValue.length() > 1 
114             && dateValue.startsWith("'") 
115             && dateValue.endsWith("'")
116         ) {
117             dateValue = dateValue.substring (1, dateValue.length() - 1);
118         }
119         
120         SimpleDateFormat dateParser = null;        
121         Iterator formatIter = dateFormats.iterator();
122         
123         while (formatIter.hasNext()) {
124             String format = (String) formatIter.next();            
125             if (dateParser == null) {
126                 dateParser = new SimpleDateFormat(format, Locale.US);
127                 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
128             } else {
129                 dateParser.applyPattern(format);                    
130             }
131             try {
132                 return dateParser.parse(dateValue);
133             } catch (ParseException pe) {
134                 // ignore this exception, we will try the next format
135             }                
136         }
137         
138         // we were unable to parse the date
139         throw new DateParseException("Unable to parse the date " + dateValue);        
140     }
141 
142     /*** This class should not be instantiated. */    
143     private DateParser() { }
144     
145 }