FileDocCategorySizeDatePackage
DateUtils.javaAPI DocAndroid 1.5 API9463Wed May 06 22:41:10 BST 2009org.apache.http.impl.cookie

DateUtils

public final class DateUtils extends Object
A utility class for parsing and formatting HTTP dates as used in cookies and other headers. This class handles dates as defined by RFC 2616 section 3.3.1 as well as some other common non-standard formats.
author
Christopher Brown
author
Michael Becke

Fields Summary
public static final String
PATTERN_RFC1123
Date format pattern used to parse HTTP date headers in RFC 1123 format.
public static final String
PATTERN_RFC1036
Date format pattern used to parse HTTP date headers in RFC 1036 format.
public static final String
PATTERN_ASCTIME
Date format pattern used to parse HTTP date headers in ANSI C asctime() format.
private static final String[]
DEFAULT_PATTERNS
private static final Date
DEFAULT_TWO_DIGIT_YEAR_START
public static final TimeZone
GMT
Constructors Summary
private DateUtils()
This class should not be instantiated.

 
    
Methods Summary
public static java.lang.StringformatDate(java.util.Date date)
Formats the given date according to the RFC 1123 pattern.

param
date The date to format.
return
An RFC 1123 formatted date string.
see
#PATTERN_RFC1123

        return formatDate(date, PATTERN_RFC1123);
    
public static java.lang.StringformatDate(java.util.Date date, java.lang.String pattern)
Formats the given date according to the specified pattern. The pattern must conform to that used by the {@link SimpleDateFormat simple date format} class.

param
date The date to format.
param
pattern The pattern to use for formatting the date.
return
A formatted date string.
throws
IllegalArgumentException If the given date pattern is invalid.
see
SimpleDateFormat

        if (date == null) throw new IllegalArgumentException("date is null");
        if (pattern == null) throw new IllegalArgumentException("pattern is null");
        
        SimpleDateFormat formatter = DateFormatHolder.formatFor(pattern);
        return formatter.format(date);
    
public static java.util.DateparseDate(java.lang.String dateValue)
Parses a date value. The formats used for parsing the date value are retrieved from the default http params.

param
dateValue the date value to parse
return
the parsed date
throws
DateParseException if the value could not be parsed using any of the supported date formats

    
     
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(GMT);
        calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); 
    
        return parseDate(dateValue, null, null);
    
public static java.util.DateparseDate(java.lang.String dateValue, java.lang.String[] dateFormats)
Parses the date value using the given date formats.

param
dateValue the date value to parse
param
dateFormats the date formats to use
return
the parsed date
throws
DateParseException if none of the dataFormats could parse the dateValue

        return parseDate(dateValue, dateFormats, null);
    
public static java.util.DateparseDate(java.lang.String dateValue, java.lang.String[] dateFormats, java.util.Date startDate)
Parses the date value using the given date formats.

param
dateValue the date value to parse
param
dateFormats the date formats to use
param
startDate During parsing, two digit years will be placed in the range startDate to startDate + 100 years. This value may be null. When null is given as a parameter, year 2000 will be used.
return
the parsed date
throws
DateParseException if none of the dataFormats could parse the dateValue

        
        if (dateValue == null) {
            throw new IllegalArgumentException("dateValue is null");
        }
        if (dateFormats == null) {
            dateFormats = DEFAULT_PATTERNS;
        }
        if (startDate == null) {
            startDate = DEFAULT_TWO_DIGIT_YEAR_START;
        }
        // trim single quotes around date if present
        // see issue #5279
        if (dateValue.length() > 1 
            && dateValue.startsWith("'") 
            && dateValue.endsWith("'")
        ) {
            dateValue = dateValue.substring (1, dateValue.length() - 1);
        }

        for (String dateFormat : dateFormats) {
            SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat);
            dateParser.set2DigitYearStart(startDate);

            try {
                return dateParser.parse(dateValue);
            } catch (ParseException pe) {
                // ignore this exception, we will try the next format
            }
        }
        
        // we were unable to parse the date
        throw new DateParseException("Unable to parse the date " + dateValue);