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