View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java,v 1.3.2.2 2004/02/22 18:21:16 olegk Exp $
3    * $Revision: 1.3.2.2 $
4    * $Date: 2004/02/22 18:21:16 $
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   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient.util;
33  
34  import java.text.ParseException;
35  import java.text.SimpleDateFormat;
36  import java.util.Date;
37  import java.util.Locale;
38  import java.util.TimeZone;
39  
40  /***
41   * A utility class for parsing HTTP dates as used in cookies and other headers.  
42   * This class handles dates as defined by RFC 2616 section 3.3.1 as well as 
43   * some other common non-standard formats.
44   * 
45   * @author Christopher Brown
46   * @author Michael Becke
47   */
48  public class DateParser {
49  
50      /***
51       * Date format pattern used to parse HTTP date headers in RFC 1123 format.
52       */
53      public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
54  
55      /***
56       * Date format pattern used to parse HTTP date headers in RFC 1036 format.
57       */
58      public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
59  
60      /***
61       * Date format pattern used to parse HTTP date headers in ANSI C 
62       * <code>asctime()</code> format.
63       */
64      public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
65  
66      /*** The patterns used for parsing dates */
67      private static final String[] DATE_PATTERNS = {
68          PATTERN_RFC1123,
69          PATTERN_RFC1036,
70          PATTERN_ASCTIME,
71          "EEE, dd-MMM-yyyy HH:mm:ss z",
72          "EEE, dd-MMM-yyyy HH-mm-ss z",
73          "EEE, dd MMM yy HH:mm:ss z",
74          "EEE dd-MMM-yyyy HH:mm:ss z",
75          "EEE dd MMM yyyy HH:mm:ss z",
76          "EEE dd-MMM-yyyy HH-mm-ss z",
77          "EEE dd-MMM-yy HH:mm:ss z",
78          "EEE dd MMM yy HH:mm:ss z",
79          "EEE,dd-MMM-yy HH:mm:ss z",
80          "EEE,dd-MMM-yyyy HH:mm:ss z",
81          "EEE, dd-MM-yyyy HH:mm:ss z",
82      };
83  
84      /***
85       * Parses a date value.
86       *
87       * @param dateValue the date value to parse
88       * 
89       * @return the parsed date
90       *
91       * @throws DateParseException if the value could not be parsed using any of the 
92       * supported date formats
93       */
94      public static Date parseDate(String dateValue) throws DateParseException {
95          return parseDate(dateValue, DATE_PATTERNS);
96      }
97      
98      /***
99       * Parses the date value using the array of date formats.
100      * 
101      * @param dateValue the date value to parse
102      * @param dateFormats the date formats to use
103      * 
104      * @return the parsed date
105      * 
106      * @throws DateParseException if none of the dataFormats could parse the dateValue
107      */
108     private static Date parseDate(
109         String dateValue, 
110         String[] dateFormats
111     ) throws DateParseException {
112         
113         if (dateValue == null) {
114             throw new IllegalArgumentException("dateValue is null");
115         }
116         
117         SimpleDateFormat dateParser = null;
118         
119         for (int i = 0; i < dateFormats.length; i++) {
120             if (dateParser == null) {
121                 dateParser = new SimpleDateFormat(dateFormats[i], Locale.US);
122                 dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
123             } else {
124                 dateParser.applyPattern(dateFormats[i]);                    
125             }
126             try {
127                 return dateParser.parse(dateValue);
128             } catch (ParseException pe) {
129                 // ignore this exception, we will try the next format
130             }                
131         }
132         
133         // we were unable to parse the date
134         throw new DateParseException("Unable to parse the date " + dateValue);        
135     }
136 
137     /*** This class should not be instantiated. */    
138     private DateParser() { }
139     
140 }