Top Description Fields Constructors Methods
org.apache.hc.client5.http.utils

public final Class DateUtils

extends Object
Class Inheritance
Imports
java.time.Instant, .LocalDateTime, .ZoneId, .ZoneOffset, java.time.format.DateTimeFormatter, .DateTimeFormatterBuilder, .DateTimeParseException, java.util.Date, .Locale, .TimeZone, org.apache.hc.core5.http.Header, .MessageHeaders, org.apache.hc.core5.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

Field Summary

Modifier and TypeField and Description
public static final DateTimeFormatter
FORMATTER_ASCTIME

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

public static final DateTimeFormatter
FORMATTER_RFC1036

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

public static final DateTimeFormatter
FORMATTER_RFC1123

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

public static final TimeZone
GMT

Deprecated This attribute is no longer supported as a part of the public API.
pack-priv static final ZoneId
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.

public static final DateTimeFormatter[]

Constructor Summary

AccessConstructor and Description
private
DateUtils()

This class should not be instantiated.

Method Summary

Modifier and TypeMethod and Description
public static void
clearThreadLocal()

Deprecated Noop method. Do not use.
Clears thread-local variable containing java.text.DateFormat cache.
public static String

Returns:

A formatted instant string.
formatDate
(final Instant
Instant to format.
instant
,
final DateTimeFormatter
The pattern to use for formatting the instant.
dateTimeFormatter
)

Formats the given date according to the specified pattern.

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 String

Returns:

An RFC 1123 formatted instant string.
formatStandardDate
(final Instant
Instant to format.
instant
)

Formats the given instant according to the RFC 1123 pattern.

public static boolean

Returns:

true if both messages contain a header with the given name and the value of the header from the first message is newer that of the second message.
isAfter
(final MessageHeaders
the first message
message1
,
final MessageHeaders
the second message
message2
,
final String
header name
headerName
)

Deprecated This method is no longer supported as a part of the public API.
Tests if the first message is after (newer) than second one using the given message header for comparison.
public static boolean

Returns:

true if both messages contain a header with the given name and the value of the header from the first message is older that of the second message.
isBefore
(final MessageHeaders
the first message
message1
,
final MessageHeaders
the second message
message2
,
final String
header name
headerName
)

Deprecated This method is no longer supported as a part of the public API.
Tests if the first message is before (older) than the second one using the given message header for comparison.
public static Instant

Returns:

the parsed instant or null if input could not be parsed
parseDate
(final String
the instant value to parse
dateValue
,
final DateTimeFormatter...
the date/time formats to use
dateFormatters
)

Parses the date value using the given date/time 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
)

Deprecated Use parseStandardDate(String)
Parses a date value.
public static Date

Returns:

the parsed date or null if input could not be parsed
parseDate
(final MessageHeaders
message headers
headers
,
final String
header name
headerName
)

Parses a date value from a header with the given name.
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/time formats to use
dateFormats
)

Parses the date value using the given date/time 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/time 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/time formats.
public static Instant

Returns:

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

Parses the instant value using the standard date/time formats (PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_ASCTIME).

public static Instant

Returns:

the parsed instant or null if input could not be parsed
parseStandardDate
(final MessageHeaders
message headers
headers
,
final String
header name
headerName
)

Parses an instant value from a header with the given name.

public static Date
toDate(final Instant instant)

public static Instant
toInstant(final Date date)

public static LocalDateTime
toUTC(final Instant instant)

public static LocalDateTime
toUTC(final Date date)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FORMATTER_ASCTIMEback to summary
public static final DateTimeFormatter FORMATTER_ASCTIME

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

Since
5.2
FORMATTER_RFC1036back to summary
public static final DateTimeFormatter FORMATTER_RFC1036

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

Since
5.2
FORMATTER_RFC1123back to summary
public static final DateTimeFormatter FORMATTER_RFC1123

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

Since
5.2
GMTback to summary
public static final TimeZone GMT

Deprecated

This attribute is no longer supported as a part of the public API.

Annotations
@Deprecated
GMT_IDback to summary
pack-priv static final ZoneId GMT_ID
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.

STANDARD_PATTERNSback to summary
public static final DateTimeFormatter[] STANDARD_PATTERNS

Standard date formatters: FORMATTER_RFC1123, FORMATTER_RFC1036, FORMATTER_ASCTIME.

Since
5.2

Constructor Detail

DateUtilsback to summary
private DateUtils()

This class should not be instantiated.

Method Detail

clearThreadLocalback to summary
public static void clearThreadLocal()

Deprecated

Noop method. Do not use.

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

Annotations
@Deprecated
Since
4.3
formatDateback to summary
public static String formatDate(final Instant instant, final DateTimeFormatter dateTimeFormatter)

Formats the given date according to the specified pattern.

Parameters
instant:Instant

Instant to format.

dateTimeFormatter:DateTimeFormatter

The pattern to use for formatting the instant.

Returns:String

A formatted instant string.

Exceptions
IllegalArgumentException:
If the given date pattern is invalid.
Since
5.2
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.

Annotations
@Deprecated
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.

Parameters
date:Date

The date to format.

pattern:String

The pattern to use for formatting the date.

Returns:String

A formatted date string.

Annotations
@Deprecated
Exceptions
IllegalArgumentException:
If the given date pattern is invalid.
formatStandardDateback to summary
public static String formatStandardDate(final Instant instant)

Formats the given instant according to the RFC 1123 pattern.

Parameters
instant:Instant

Instant to format.

Returns:String

An RFC 1123 formatted instant string.

Since
5.2
See Also
PATTERN_RFC1123
isAfterback to summary
public static boolean isAfter(final MessageHeaders message1, final MessageHeaders message2, final String headerName)

Deprecated

This method is no longer supported as a part of the public API.

Tests if the first message is after (newer) than second one using the given message header for comparison.

Parameters
message1:MessageHeaders

the first message

message2:MessageHeaders

the second message

headerName:String

header name

Returns:boolean

true if both messages contain a header with the given name and the value of the header from the first message is newer that of the second message.

Annotations
@Deprecated
Since
5.0
isBeforeback to summary
public static boolean isBefore(final MessageHeaders message1, final MessageHeaders message2, final String headerName)

Deprecated

This method is no longer supported as a part of the public API.

Tests if the first message is before (older) than the second one using the given message header for comparison.

Parameters
message1:MessageHeaders

the first message

message2:MessageHeaders

the second message

headerName:String

header name

Returns:boolean

true if both messages contain a header with the given name and the value of the header from the first message is older that of the second message.

Annotations
@Deprecated
Since
5.0
parseDateback to summary
public static Instant parseDate(final String dateValue, final DateTimeFormatter... dateFormatters)

Parses the date value using the given date/time formats.

Parameters
dateValue:String

the instant value to parse

dateFormatters:DateTimeFormatter[]

the date/time formats to use

Returns:Instant

the parsed instant or null if input could not be parsed

Since
5.2
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

Annotations
@Deprecated
parseDateback to summary
public static Date parseDate(final MessageHeaders headers, final String headerName)

Parses a date value from a header with the given name.

Parameters
headers:MessageHeaders

message headers

headerName:String

header name

Returns:Date

the parsed date or null if input could not be parsed

Annotations
@Deprecated
Since
5.0
parseDateback to summary
public static Date parseDate(final String dateValue, final String[] dateFormats)

Parses the date value using the given date/time formats.

Parameters
dateValue:String

the date value to parse

dateFormats:String[]

the date/time formats to use

Returns:Date

the parsed date or null if input could not be parsed

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

Parses the date value using the given date/time formats.

Parameters
dateValue:String

the date value to parse

dateFormats:String[]

the date/time 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

Annotations
@Deprecated
parseStandardDateback to summary
public static Instant parseStandardDate(final String dateValue)

Parses the instant value using the standard date/time formats (PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_ASCTIME).

Parameters
dateValue:String

the instant value to parse

Returns:Instant

the parsed instant or null if input could not be parsed

Since
5.2
parseStandardDateback to summary
public static Instant parseStandardDate(final MessageHeaders headers, final String headerName)

Parses an instant value from a header with the given name.

Parameters
headers:MessageHeaders

message headers

headerName:String

header name

Returns:Instant

the parsed instant or null if input could not be parsed

Since
5.2
toDateback to summary
public static Date toDate(final Instant instant)
Since
5.2
toInstantback to summary
public static Instant toInstant(final Date date)
Since
5.2
toUTCback to summary
public static LocalDateTime toUTC(final Instant instant)
Since
5.2
toUTCback to summary
public static LocalDateTime toUTC(final Date date)
Since
5.2