1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
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                 
130             }                
131         }
132         
133         
134         throw new DateParseException("Unable to parse the date " + dateValue);        
135     }
136 
137     /*** This class should not be instantiated. */    
138     private DateParser() { }
139     
140 }