View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.cookie;
29  
30  import java.text.SimpleDateFormat;
31  import java.util.Date;
32  import java.util.TimeZone;
33  
34  import org.apache.http.annotation.Immutable;
35  
36  /**
37   * A utility class for parsing and formatting HTTP dates as used in cookies and
38   * other headers.  This class handles dates as defined by RFC 2616 section
39   * 3.3.1 as well as some other common non-standard formats.
40   *
41   *
42   * @since 4.0
43   *
44   * @deprecated (4.3) Use {@link org.apache.http.client.utils.DateUtils}.
45   */
46  @Deprecated
47  @Immutable
48  public final class DateUtils {
49  
50      /**
51       * Date format pattern used to parse HTTP date headers in RFC 1123 format.
52       */
53      public static final String PATTERN_RFC1123 = org.apache.http.client.utils.DateUtils.PATTERN_RFC1123;
54  
55      /**
56       * Date format pattern used to parse HTTP date headers in RFC 1036 format.
57       */
58      public static final String PATTERN_RFC1036 = org.apache.http.client.utils.DateUtils.PATTERN_RFC1036;
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 = org.apache.http.client.utils.DateUtils.PATTERN_ASCTIME;
65  
66      public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
67  
68      /**
69       * Parses a date value.  The formats used for parsing the date value are retrieved from
70       * the default http params.
71       *
72       * @param dateValue the date value to parse
73       *
74       * @return the parsed date
75       *
76       * @throws DateParseException if the value could not be parsed using any of the
77       * supported date formats
78       */
79      public static Date parseDate(final String dateValue) throws DateParseException {
80          return parseDate(dateValue, null, null);
81      }
82  
83      /**
84       * Parses the date value using the given date formats.
85       *
86       * @param dateValue the date value to parse
87       * @param dateFormats the date formats to use
88       *
89       * @return the parsed date
90       *
91       * @throws DateParseException if none of the dataFormats could parse the dateValue
92       */
93      public static Date parseDate(final String dateValue, final String[] dateFormats)
94          throws DateParseException {
95          return parseDate(dateValue, dateFormats, null);
96      }
97  
98      /**
99       * Parses the date value using the given date formats.
100      *
101      * @param dateValue the date value to parse
102      * @param dateFormats the date formats to use
103      * @param startDate During parsing, two digit years will be placed in the range
104      * <code>startDate</code> to <code>startDate + 100 years</code>. This value may
105      * be <code>null</code>. When <code>null</code> is given as a parameter, year
106      * <code>2000</code> will be used.
107      *
108      * @return the parsed date
109      *
110      * @throws DateParseException if none of the dataFormats could parse the dateValue
111      */
112     public static Date parseDate(
113         final String dateValue,
114         final String[] dateFormats,
115         final Date startDate
116     ) throws DateParseException {
117         final Date d = org.apache.http.client.utils.DateUtils.parseDate(dateValue, dateFormats, startDate);
118         if (d == null) {
119             throw new DateParseException("Unable to parse the date " + dateValue);
120         }
121         return d;
122     }
123 
124     /**
125      * Formats the given date according to the RFC 1123 pattern.
126      *
127      * @param date The date to format.
128      * @return An RFC 1123 formatted date string.
129      *
130      * @see #PATTERN_RFC1123
131      */
132     public static String formatDate(final Date date) {
133         return org.apache.http.client.utils.DateUtils.formatDate(date);
134     }
135 
136     /**
137      * Formats the given date according to the specified pattern.  The pattern
138      * must conform to that used by the {@link SimpleDateFormat simple date
139      * format} class.
140      *
141      * @param date The date to format.
142      * @param pattern The pattern to use for formatting the date.
143      * @return A formatted date string.
144      *
145      * @throws IllegalArgumentException If the given date pattern is invalid.
146      *
147      * @see SimpleDateFormat
148      */
149     public static String formatDate(final Date date, final String pattern) {
150         return org.apache.http.client.utils.DateUtils.formatDate(date, pattern);
151     }
152 
153     /** This class should not be instantiated. */
154     private DateUtils() {
155     }
156 
157 }