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  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         
109         
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                 
132             }                
133         }
134         
135         
136         throw new DateParseException("Unable to parse the date " + dateValue);        
137     }
138 
139     /*** This class should not be instantiated. */    
140     private DateParser() { }
141     
142 }