Home | History | Annotate | Download | only in cookie
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java $
      3  * $Revision: 677240 $
      4  * $Date: 2008-07-16 04:25:47 -0700 (Wed, 16 Jul 2008) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.impl.cookie;
     33 
     34 import java.lang.ref.SoftReference;
     35 import java.text.ParseException;
     36 import java.text.SimpleDateFormat;
     37 import java.util.Calendar;
     38 import java.util.Date;
     39 import java.util.HashMap;
     40 import java.util.Locale;
     41 import java.util.Map;
     42 import java.util.TimeZone;
     43 
     44 /**
     45  * A utility class for parsing and formatting HTTP dates as used in cookies and
     46  * other headers.  This class handles dates as defined by RFC 2616 section
     47  * 3.3.1 as well as some other common non-standard formats.
     48  *
     49  * @author Christopher Brown
     50  * @author Michael Becke
     51  *
     52  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     53  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     54  *     for further details.
     55  */
     56 @Deprecated
     57 public final class DateUtils {
     58 
     59     /**
     60      * Date format pattern used to parse HTTP date headers in RFC 1123 format.
     61      */
     62     public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
     63 
     64     /**
     65      * Date format pattern used to parse HTTP date headers in RFC 1036 format.
     66      */
     67     public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
     68 
     69     /**
     70      * Date format pattern used to parse HTTP date headers in ANSI C
     71      * <code>asctime()</code> format.
     72      */
     73     public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
     74 
     75     private static final String[] DEFAULT_PATTERNS = new String[] {
     76     	PATTERN_RFC1036,
     77     	PATTERN_RFC1123,
     78         PATTERN_ASCTIME
     79     };
     80 
     81     private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
     82 
     83     public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
     84 
     85     static {
     86         Calendar calendar = Calendar.getInstance();
     87         calendar.setTimeZone(GMT);
     88         calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
     89         calendar.set(Calendar.MILLISECOND, 0);
     90         DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime();
     91     }
     92 
     93     /**
     94      * Parses a date value.  The formats used for parsing the date value are retrieved from
     95      * the default http params.
     96      *
     97      * @param dateValue the date value to parse
     98      *
     99      * @return the parsed date
    100      *
    101      * @throws DateParseException if the value could not be parsed using any of the
    102      * supported date formats
    103      */
    104     public static Date parseDate(String dateValue) throws DateParseException {
    105         return parseDate(dateValue, null, null);
    106     }
    107 
    108     /**
    109      * Parses the date value using the given date formats.
    110      *
    111      * @param dateValue the date value to parse
    112      * @param dateFormats the date formats to use
    113      *
    114      * @return the parsed date
    115      *
    116      * @throws DateParseException if none of the dataFormats could parse the dateValue
    117      */
    118     public static Date parseDate(final String dateValue, String[] dateFormats)
    119         throws DateParseException {
    120         return parseDate(dateValue, dateFormats, null);
    121     }
    122 
    123     /**
    124      * Parses the date value using the given date formats.
    125      *
    126      * @param dateValue the date value to parse
    127      * @param dateFormats the date formats to use
    128      * @param startDate During parsing, two digit years will be placed in the range
    129      * <code>startDate</code> to <code>startDate + 100 years</code>. This value may
    130      * be <code>null</code>. When <code>null</code> is given as a parameter, year
    131      * <code>2000</code> will be used.
    132      *
    133      * @return the parsed date
    134      *
    135      * @throws DateParseException if none of the dataFormats could parse the dateValue
    136      */
    137     public static Date parseDate(
    138         String dateValue,
    139         String[] dateFormats,
    140         Date startDate
    141     ) throws DateParseException {
    142 
    143         if (dateValue == null) {
    144             throw new IllegalArgumentException("dateValue is null");
    145         }
    146         if (dateFormats == null) {
    147             dateFormats = DEFAULT_PATTERNS;
    148         }
    149         if (startDate == null) {
    150             startDate = DEFAULT_TWO_DIGIT_YEAR_START;
    151         }
    152         // trim single quotes around date if present
    153         // see issue #5279
    154         if (dateValue.length() > 1
    155             && dateValue.startsWith("'")
    156             && dateValue.endsWith("'")
    157         ) {
    158             dateValue = dateValue.substring (1, dateValue.length() - 1);
    159         }
    160 
    161         for (String dateFormat : dateFormats) {
    162             SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
    163             dateParser.set2DigitYearStart(startDate);
    164 
    165             try {
    166                 return dateParser.parse(dateValue);
    167             } catch (ParseException pe) {
    168                 // ignore this exception, we will try the next format
    169             }
    170         }
    171 
    172         // we were unable to parse the date
    173         throw new DateParseException("Unable to parse the date " + dateValue);
    174     }
    175 
    176     /**
    177      * Formats the given date according to the RFC 1123 pattern.
    178      *
    179      * @param date The date to format.
    180      * @return An RFC 1123 formatted date string.
    181      *
    182      * @see #PATTERN_RFC1123
    183      */
    184     public static String formatDate(Date date) {
    185         return formatDate(date, PATTERN_RFC1123);
    186     }
    187 
    188     /**
    189      * Formats the given date according to the specified pattern.  The pattern
    190      * must conform to that used by the {@link SimpleDateFormat simple date
    191      * format} class.
    192      *
    193      * @param date The date to format.
    194      * @param pattern The pattern to use for formatting the date.
    195      * @return A formatted date string.
    196      *
    197      * @throws IllegalArgumentException If the given date pattern is invalid.
    198      *
    199      * @see SimpleDateFormat
    200      */
    201     public static String formatDate(Date date, String pattern) {
    202         if (date == null) throw new IllegalArgumentException("date is null");
    203         if (pattern == null) throw new IllegalArgumentException("pattern is null");
    204 
    205         SimpleDateFormat formatter = DateFormatHolder.formatFor(pattern);
    206         return formatter.format(date);
    207     }
    208 
    209     /** This class should not be instantiated. */
    210     private DateUtils() {
    211     }
    212 
    213     /**
    214      * A factory for {@link SimpleDateFormat}s. The instances are stored in a
    215      * threadlocal way because SimpleDateFormat is not threadsafe as noted in
    216      * {@link SimpleDateFormat its javadoc}.
    217      *
    218      * @author Daniel Mueller
    219      */
    220     final static class DateFormatHolder {
    221 
    222         private static final ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>>
    223             THREADLOCAL_FORMATS = new ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>>() {
    224 
    225             @Override
    226             protected SoftReference<Map<String, SimpleDateFormat>> initialValue() {
    227                 return new SoftReference<Map<String, SimpleDateFormat>>(
    228                         new HashMap<String, SimpleDateFormat>());
    229             }
    230 
    231         };
    232 
    233         /**
    234          * creates a {@link SimpleDateFormat} for the requested format string.
    235          *
    236          * @param pattern
    237          *            a non-<code>null</code> format String according to
    238          *            {@link SimpleDateFormat}. The format is not checked against
    239          *            <code>null</code> since all paths go through
    240          *            {@link DateUtils}.
    241          * @return the requested format. This simple dateformat should not be used
    242          *         to {@link SimpleDateFormat#applyPattern(String) apply} to a
    243          *         different pattern.
    244          */
    245         public static SimpleDateFormat formatFor(String pattern) {
    246             SoftReference<Map<String, SimpleDateFormat>> ref = THREADLOCAL_FORMATS.get();
    247             Map<String, SimpleDateFormat> formats = ref.get();
    248             if (formats == null) {
    249                 formats = new HashMap<String, SimpleDateFormat>();
    250                 THREADLOCAL_FORMATS.set(
    251                         new SoftReference<Map<String, SimpleDateFormat>>(formats));
    252             }
    253 
    254             SimpleDateFormat format = formats.get(pattern);
    255             if (format == null) {
    256                 format = new SimpleDateFormat(pattern, Locale.US);
    257                 format.setTimeZone(TimeZone.getTimeZone("GMT"));
    258                 formats.put(pattern, format);
    259             }
    260 
    261             return format;
    262         }
    263 
    264     }
    265 
    266 }
    267