Top Description Inners Fields Constructors Methods
org.apache.http.client.utils

public final Class DateUtils

extends Object
Class Inheritance
Imports
java.lang.ref.SoftReference, java.text.ParsePosition, .SimpleDateFormat, java.util.Calendar, .Date, .HashMap, .Locale, .Map, .TimeZone, org.apache.http.util.Args

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.
Since
4.3

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final String[]
private static final Date
public static final TimeZone
public static final String
PATTERN_ASCTIME

Date format pattern used to parse HTTP date headers in ANSI C asctime() 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_RFC1123

Date format pattern used to parse HTTP date headers in RFC 1123 format.

Constructor Summary

AccessConstructor and Description
private
DateUtils()

This class should not be instantiated.

Method Summary

Modifier and TypeMethod and Description
public static void
clearThreadLocal()

Clears thread-local variable containing java.text.DateFormat cache.

public static String

Returns:

An RFC 1123 formatted date string.
formatDate
(final Date
The date to format.
date
)

Formats the given date according to the RFC 1123 pattern.

public static String

Returns:

A formatted date string.
formatDate
(final Date
The date to format.
date
,
final String
The pattern to use for formatting the date.
pattern
)

Formats the given date according to the specified pattern.

public static Date

Returns:

the parsed date or null if input could not be parsed
parseDate
(final String
the date value to parse
dateValue
)

Parses a date value.

public static Date

Returns:

the parsed date or null if input could not be parsed
parseDate
(final String
the date value to parse
dateValue
,
final String[]
the date formats to use
dateFormats
)

Parses the date value using the given date formats.

public static Date

Returns:

the parsed date or null if input could not be parsed
parseDate
(final String
the date value to parse
dateValue
,
final String[]
the date formats to use
dateFormats
,
final Date
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.
startDate
)

Parses the date value using the given date formats.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_PATTERNSback to summary
private static final String[] DEFAULT_PATTERNS
DEFAULT_TWO_DIGIT_YEAR_STARTback to summary
private static final Date DEFAULT_TWO_DIGIT_YEAR_START
GMTback to summary
public static final TimeZone GMT
PATTERN_ASCTIMEback to summary
public static final String PATTERN_ASCTIME

Date format pattern used to parse HTTP date headers in ANSI C asctime() format.

PATTERN_RFC1036back to summary
public static final String PATTERN_RFC1036

Date format pattern used to parse HTTP date headers in RFC 1036 format.

PATTERN_RFC1123back to summary
public static final String PATTERN_RFC1123

Date format pattern used to parse HTTP date headers in RFC 1123 format.

Constructor Detail

DateUtilsback to summary
private DateUtils()

This class should not be instantiated.

Method Detail

clearThreadLocalback to summary
public static void clearThreadLocal()

Clears thread-local variable containing java.text.DateFormat cache.

Since
4.3
formatDateback to summary
public static String formatDate(final Date date)

Formats the given date according to the RFC 1123 pattern.

Parameters
date:Date

The date to format.

Returns:String

An RFC 1123 formatted date string.

See Also
PATTERN_RFC1123
formatDateback to summary
public static String formatDate(final Date date, final String pattern)

Formats the given date according to the specified pattern. The pattern must conform to that used by the simple date format class.

Parameters
date:Date

The date to format.

pattern:String

The pattern to use for formatting the date.

Returns:String

A formatted date string.

Exceptions
IllegalArgumentException:
If the given date pattern is invalid.
See Also
SimpleDateFormat
parseDateback to summary
public static Date parseDate(final String dateValue)

Parses a date value. The formats used for parsing the date value are retrieved from the default http params.

Parameters
dateValue:String

the date value to parse

Returns:Date

the parsed date or null if input could not be parsed

parseDateback to summary
public static Date parseDate(final String dateValue, final String[] dateFormats)

Parses the date value using the given date formats.

Parameters
dateValue:String

the date value to parse

dateFormats:String[]

the date formats to use

Returns:Date

the parsed date or null if input could not be parsed

parseDateback to summary
public static Date parseDate(final String dateValue, final String[] dateFormats, final Date startDate)

Parses the date value using the given date formats.

Parameters
dateValue:String

the date value to parse

dateFormats:String[]

the date formats to use

startDate:Date

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.

Returns:Date

the parsed date or null if input could not be parsed

org.apache.http.client.utils back to summary

pack-priv final Class DateUtils.DateFormatHolder

extends Object
Class Inheritance

A factory for SimpleDateFormats. The instances are stored in a threadlocal way because SimpleDateFormat is not threadsafe as noted in its javadoc.

Field Summary

Modifier and TypeField and Description
private static final ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>>

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public static void
public static SimpleDateFormat

Returns:

the requested format. This simple dateformat should not be used to apply to a different pattern.
formatFor
(final String
a non-null format String according to SimpleDateFormat. The format is not checked against null since all paths go through DateUtils.
pattern
)

creates a SimpleDateFormat for the requested format string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

THREADLOCAL_FORMATSback to summary
private static final ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>> THREADLOCAL_FORMATS

Constructor Detail

DateFormatHolderback to summary
pack-priv DateFormatHolder()

Method Detail

clearThreadLocalback to summary
public static void clearThreadLocal()
formatForback to summary
public static SimpleDateFormat formatFor(final String pattern)

creates a SimpleDateFormat for the requested format string.

Parameters
pattern:String

a non-null format String according to SimpleDateFormat. The format is not checked against null since all paths go through DateUtils.

Returns:SimpleDateFormat

the requested format. This simple dateformat should not be used to apply to a different pattern.