Class DateUtils


  • public final class DateUtils
    extends java.lang.Object
    A utility class for parsing and formatting HTTP dates as used in cookies and other headers.
    Since:
    4.3
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.time.format.DateTimeFormatter FORMATTER_ASCTIME
      Date formatter used to parse HTTP date headers in in ANSI C asctime() format.
      static java.time.format.DateTimeFormatter FORMATTER_RFC1036
      Date formatter used to parse HTTP date headers in RFC 1036 format.
      static java.time.format.DateTimeFormatter FORMATTER_RFC1123
      Date formatter used to parse HTTP date headers in the Internet Message Format specified by the HTTP protocol.
      static java.util.TimeZone GMT
      Deprecated.
      This attribute is no longer supported as a part of the public API.
      (package private) static java.time.ZoneId GMT_ID  
      static java.lang.String INTERNET_MESSAGE_FORMAT  
      static java.lang.String PATTERN_ASCTIME
      Date format pattern used to parse HTTP date headers in ANSI C asctime() format.
      static java.lang.String PATTERN_RFC1036
      Date format pattern used to parse HTTP date headers in RFC 1036 format.
      static java.lang.String PATTERN_RFC1123
      Deprecated.
      static java.time.format.DateTimeFormatter[] STANDARD_PATTERNS
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private DateUtils()
      This class should not be instantiated.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void clearThreadLocal()
      Deprecated.
      Noop method.
      static java.lang.String formatDate​(java.time.Instant instant, java.time.format.DateTimeFormatter dateTimeFormatter)
      Formats the given date according to the specified pattern.
      static java.lang.String formatDate​(java.util.Date date)
      static java.lang.String formatDate​(java.util.Date date, java.lang.String pattern)
      static java.lang.String formatStandardDate​(java.time.Instant instant)
      Formats the given instant according to the RFC 1123 pattern.
      static boolean isAfter​(org.apache.hc.core5.http.MessageHeaders message1, org.apache.hc.core5.http.MessageHeaders message2, java.lang.String headerName)
      Deprecated.
      This method is no longer supported as a part of the public API.
      static boolean isBefore​(org.apache.hc.core5.http.MessageHeaders message1, org.apache.hc.core5.http.MessageHeaders message2, java.lang.String headerName)
      Deprecated.
      This method is no longer supported as a part of the public API.
      static java.util.Date parseDate​(java.lang.String dateValue)
      Deprecated.
      static java.util.Date parseDate​(java.lang.String dateValue, java.lang.String[] dateFormats)
      static java.util.Date parseDate​(java.lang.String dateValue, java.lang.String[] dateFormats, java.util.Date startDate)
      static java.time.Instant parseDate​(java.lang.String dateValue, java.time.format.DateTimeFormatter... dateFormatters)
      Parses the date value using the given date/time formats.
      static java.util.Date parseDate​(org.apache.hc.core5.http.MessageHeaders headers, java.lang.String headerName)
      static java.time.Instant parseStandardDate​(java.lang.String dateValue)
      Parses the instant value using the standard date/time formats (PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_ASCTIME).
      static java.time.Instant parseStandardDate​(org.apache.hc.core5.http.MessageHeaders headers, java.lang.String headerName)
      Parses an instant value from a header with the given name.
      static java.util.Date toDate​(java.time.Instant instant)  
      static java.time.Instant toInstant​(java.util.Date date)  
      static java.time.LocalDateTime toUTC​(java.time.Instant instant)  
      static java.time.LocalDateTime toUTC​(java.util.Date date)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INTERNET_MESSAGE_FORMAT

        public static final java.lang.String INTERNET_MESSAGE_FORMAT
        See Also:
        Constant Field Values
      • FORMATTER_RFC1123

        public static final java.time.format.DateTimeFormatter FORMATTER_RFC1123
        Date formatter used to parse HTTP date headers in the Internet Message Format specified by the HTTP protocol.
        Since:
        5.2
      • PATTERN_RFC1036

        public static final java.lang.String PATTERN_RFC1036
        Date format pattern used to parse HTTP date headers in RFC 1036 format.
        See Also:
        Constant Field Values
      • FORMATTER_RFC1036

        public static final java.time.format.DateTimeFormatter FORMATTER_RFC1036
        Date formatter used to parse HTTP date headers in RFC 1036 format.
        Since:
        5.2
      • PATTERN_ASCTIME

        public static final java.lang.String PATTERN_ASCTIME
        Date format pattern used to parse HTTP date headers in ANSI C asctime() format.
        See Also:
        Constant Field Values
      • FORMATTER_ASCTIME

        public static final java.time.format.DateTimeFormatter FORMATTER_ASCTIME
        Date formatter used to parse HTTP date headers in in ANSI C asctime() format.
        Since:
        5.2
      • GMT_ID

        static final java.time.ZoneId GMT_ID
      • GMT

        @Deprecated
        public static final java.util.TimeZone GMT
        Deprecated.
        This attribute is no longer supported as a part of the public API.
    • Constructor Detail

      • DateUtils

        private DateUtils()
        This class should not be instantiated.
    • Method Detail

      • toDate

        public static java.util.Date toDate​(java.time.Instant instant)
        Since:
        5.2
      • toInstant

        public static java.time.Instant toInstant​(java.util.Date date)
        Since:
        5.2
      • toUTC

        public static java.time.LocalDateTime toUTC​(java.time.Instant instant)
        Since:
        5.2
      • toUTC

        public static java.time.LocalDateTime toUTC​(java.util.Date date)
        Since:
        5.2
      • parseDate

        public static java.time.Instant parseDate​(java.lang.String dateValue,
                                                  java.time.format.DateTimeFormatter... dateFormatters)
        Parses the date value using the given date/time formats.

        This method can handle strings without time-zone information by failing gracefully, in which case it returns null.

        Parameters:
        dateValue - the instant value to parse
        dateFormatters - the date/time formats to use
        Returns:
        the parsed instant or null if input could not be parsed
        Since:
        5.2
      • parseStandardDate

        public static java.time.Instant parseStandardDate​(java.lang.String dateValue)
        Parses the instant value using the standard date/time formats (PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_ASCTIME).
        Parameters:
        dateValue - the instant value to parse
        Returns:
        the parsed instant or null if input could not be parsed
        Since:
        5.2
      • parseStandardDate

        public static java.time.Instant parseStandardDate​(org.apache.hc.core5.http.MessageHeaders headers,
                                                          java.lang.String headerName)
        Parses an instant value from a header with the given name.
        Parameters:
        headers - message headers
        headerName - header name
        Returns:
        the parsed instant or null if input could not be parsed
        Since:
        5.2
      • formatStandardDate

        public static java.lang.String formatStandardDate​(java.time.Instant instant)
        Formats the given instant according to the RFC 1123 pattern.
        Parameters:
        instant - Instant to format.
        Returns:
        An RFC 1123 formatted instant string.
        Since:
        5.2
        See Also:
        PATTERN_RFC1123
      • formatDate

        public static java.lang.String formatDate​(java.time.Instant instant,
                                                  java.time.format.DateTimeFormatter dateTimeFormatter)
        Formats the given date according to the specified pattern.
        Parameters:
        instant - Instant to format.
        dateTimeFormatter - The pattern to use for formatting the instant.
        Returns:
        A formatted instant string.
        Throws:
        java.lang.IllegalArgumentException - If the given date pattern is invalid.
        Since:
        5.2
      • parseDate

        @Deprecated
        public static java.util.Date parseDate​(java.lang.String dateValue)
        Deprecated.
        Parses a date value. The formats used for parsing the date value are retrieved from the default http params.
        Parameters:
        dateValue - the date value to parse
        Returns:
        the parsed date or null if input could not be parsed
      • parseDate

        @Deprecated
        public static java.util.Date parseDate​(org.apache.hc.core5.http.MessageHeaders headers,
                                               java.lang.String headerName)
        Parses a date value from a header with the given name.
        Parameters:
        headers - message headers
        headerName - header name
        Returns:
        the parsed date or null if input could not be parsed
        Since:
        5.0
      • isAfter

        @Deprecated
        public static boolean isAfter​(org.apache.hc.core5.http.MessageHeaders message1,
                                      org.apache.hc.core5.http.MessageHeaders message2,
                                      java.lang.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 - the first message
        message2 - the second message
        headerName - header name
        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.
        Since:
        5.0
      • isBefore

        @Deprecated
        public static boolean isBefore​(org.apache.hc.core5.http.MessageHeaders message1,
                                       org.apache.hc.core5.http.MessageHeaders message2,
                                       java.lang.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 - the first message
        message2 - the second message
        headerName - header name
        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.
        Since:
        5.0
      • parseDate

        @Deprecated
        public static java.util.Date parseDate​(java.lang.String dateValue,
                                               java.lang.String[] dateFormats)
        Parses the date value using the given date/time formats.
        Parameters:
        dateValue - the date value to parse
        dateFormats - the date/time formats to use
        Returns:
        the parsed date or null if input could not be parsed
      • parseDate

        @Deprecated
        public static java.util.Date parseDate​(java.lang.String dateValue,
                                               java.lang.String[] dateFormats,
                                               java.util.Date startDate)
        Parses the date value using the given date/time formats.
        Parameters:
        dateValue - the date value to parse
        dateFormats - the date/time formats to use
        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.
        Returns:
        the parsed date or null if input could not be parsed
      • formatDate

        @Deprecated
        public static java.lang.String formatDate​(java.util.Date date)
        Formats the given date according to the RFC 1123 pattern.
        Parameters:
        date - The date to format.
        Returns:
        An RFC 1123 formatted date string.
        See Also:
        PATTERN_RFC1123
      • formatDate

        @Deprecated
        public static java.lang.String formatDate​(java.util.Date date,
                                                  java.lang.String pattern)
        Formats the given date according to the specified pattern.
        Parameters:
        date - The date to format.
        pattern - The pattern to use for formatting the date.
        Returns:
        A formatted date string.
        Throws:
        java.lang.IllegalArgumentException - If the given date pattern is invalid.
      • clearThreadLocal

        @Deprecated
        public static void clearThreadLocal()
        Deprecated.
        Noop method. Do not use.
        Clears thread-local variable containing DateFormat cache.
        Since:
        4.3