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: 224451 $
4    * $Date: 2005-07-23 06:23:59 -0400 (Sat, 23 Jul 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  /***
42   * A utility class for parsing HTTP dates as used in cookies and other headers.  
43   * This class handles dates as defined by RFC 2616 section 3.3.1 as well as 
44   * some other common non-standard formats.
45   * 
46   * @author Christopher Brown
47   * @author Michael Becke
48   * 
49   * @deprecated Use {@link org.apache.commons.httpclient.util.DateUtil}
50   */
51  public class DateParser {
52  
53      /***
54       * Date format pattern used to parse HTTP date headers in RFC 1123 format.
55       */
56      public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
57  
58      /***
59       * Date format pattern used to parse HTTP date headers in RFC 1036 format.
60       */
61      public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
62  
63      /***
64       * Date format pattern used to parse HTTP date headers in ANSI C 
65       * <code>asctime()</code> format.
66       */
67      public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
68  
69      private static final Collection DEFAULT_PATTERNS = Arrays.asList(
70      		new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } );
71      /***
72       * Parses a date value.  The formats used for parsing the date value are retrieved from
73       * the default http params.
74       *
75       * @param dateValue the date value to parse
76       * 
77       * @return the parsed date
78       *
79       * @throws DateParseException if the value could not be parsed using any of the 
80       * supported date formats
81       */
82      public static Date parseDate(String dateValue) throws DateParseException {
83          return parseDate(dateValue, null);
84      }
85      
86      /***
87       * Parses the date value using the given date formats.
88       * 
89       * @param dateValue the date value to parse
90       * @param dateFormats the date formats to use
91       * 
92       * @return the parsed date
93       * 
94       * @throws DateParseException if none of the dataFormats could parse the dateValue
95       */
96      public static Date parseDate(
97          String dateValue, 
98          Collection dateFormats
99      ) throws DateParseException {
100         
101         if (dateValue == null) {
102             throw new IllegalArgumentException("dateValue is null");
103         }
104         if (dateFormats == null) {
105         	dateFormats = DEFAULT_PATTERNS;
106         }
107         // trim single quotes around date if present
108         // see issue #5279
109         if (dateValue.length() > 1 
110             && dateValue.startsWith("'") 
111             && dateValue.endsWith("'")
112         ) {
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 " + dateValue);        
136     }
137 
138     /*** This class should not be instantiated. */    
139     private DateParser() { }
140     
141 }