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.util.Date;
31  import java.util.TimeZone;
32  
33  import org.apache.http.annotation.Immutable;
34  
35  /**
36   * A utility class for parsing and formatting HTTP dates as used in cookies and
37   * other headers.  This class handles dates as defined by RFC 2616 section
38   * 3.3.1 as well as some other common non-standard formats.
39   *
40   *
41   * @since 4.0
42   *
43   * @deprecated (4.3) Use {@link org.apache.http.client.utils.DateUtils}.
44   */
45  @Deprecated
46  @Immutable
47  public final class DateUtils {
48  
49      /**
50       * Date format pattern used to parse HTTP date headers in RFC 1123 format.
51       */
52      public static final String PATTERN_RFC1123 = org.apache.http.client.utils.DateUtils.PATTERN_RFC1123;
53  
54      /**
55       * Date format pattern used to parse HTTP date headers in RFC 1036 format.
56       */
57      public static final String PATTERN_RFC1036 = org.apache.http.client.utils.DateUtils.PATTERN_RFC1036;
58  
59      /**
60       * Date format pattern used to parse HTTP date headers in ANSI C
61       * {@code asctime()} format.
62       */
63      public static final String PATTERN_ASCTIME = org.apache.http.client.utils.DateUtils.PATTERN_ASCTIME;
64  
65      public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
66  
67      /**
68       * Parses a date value.  The formats used for parsing the date value are retrieved from
69       * the default http params.
70       *
71       * @param dateValue the date value to parse
72       *
73       * @return the parsed date
74       *
75       * @throws DateParseException if the value could not be parsed using any of the
76       * supported date formats
77       */
78      public static Date parseDate(final String dateValue) throws DateParseException {
79          return parseDate(dateValue, null, null);
80      }
81  
82      /**
83       * Parses the date value using the given date formats.
84       *
85       * @param dateValue the date value to parse
86       * @param dateFormats the date formats to use
87       *
88       * @return the parsed date
89       *
90       * @throws DateParseException if none of the dataFormats could parse the dateValue
91       */
92      public static Date parseDate(final String dateValue, final String[] dateFormats)
93          throws DateParseException {
94          return parseDate(dateValue, dateFormats, null);
95      }
96  
97      /**
98       * Parses the date value using the given date formats.
99       *
100      * @param dateValue the date value to parse
101      * @param dateFormats the date formats to use
102      * @param startDate During parsing, two digit years will be placed in the range
103      * {@code startDate} to {@code startDate + 100 years}. This value may
104      * be {@code null}. When {@code null} is given as a parameter, year
105      * {@code 2000} will be used.
106      *
107      * @return the parsed date
108      *
109      * @throws DateParseException if none of the dataFormats could parse the dateValue
110      */
111     public static Date parseDate(
112         final String dateValue,
113         final String[] dateFormats,
114         final Date startDate
115     ) throws DateParseException {
116         final Date d = org.apache.http.client.utils.DateUtils.parseDate(dateValue, dateFormats, startDate);
117         if (d == null) {
118             throw new DateParseException("Unable to parse the date " + dateValue);
119         }
120         return d;
121     }
122 
123     /**
124      * Formats the given date according to the RFC 1123 pattern.
125      *
126      * @param date The date to format.
127      * @return An RFC 1123 formatted date string.
128      *
129      * @see #PATTERN_RFC1123
130      */
131     public static String formatDate(final Date date) {
132         return org.apache.http.client.utils.DateUtils.formatDate(date);
133     }
134 
135     /**
136      * Formats the given date according to the specified pattern.  The pattern
137      * must conform to that used by the {@link java.text.SimpleDateFormat simple
138      * date format} class.
139      *
140      * @param date The date to format.
141      * @param pattern The pattern to use for formatting the date.
142      * @return A formatted date string.
143      *
144      * @throws IllegalArgumentException If the given date pattern is invalid.
145      *
146      * @see java.text.SimpleDateFormat
147      */
148     public static String formatDate(final Date date, final String pattern) {
149         return org.apache.http.client.utils.DateUtils.formatDate(date, pattern);
150     }
151 
152     /** This class should not be instantiated. */
153     private DateUtils() {
154     }
155 
156 }