Class DateUtils

java.lang.Object
org.apache.juneau.internal.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.

This class was copied from HttpClient 4.3.

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • parseISO8601Calendar

      Parses an ISO8601 string and converts it to a Calendar.
      Parameters:
      s - The string to parse.
      Returns:
      The parsed value, or null if the string was null or empty.
    • formatDate

      public static String formatDate(Date date, 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 - The date to format.
      pattern - The pattern to use for formatting the date.
      Returns:
      A formatted date string.
      Throws:
      IllegalArgumentException - If the given date pattern is invalid.
      See Also:
    • clearThreadLocal

      public static void clearThreadLocal()
      Clears thread-local variable containing DateFormat cache.
    • toValidISO8601DT

      public static String toValidISO8601DT(String in)
      Pads out an ISO8601 string so that it can be parsed using DatatypeConverter.parseDateTime(String).
      • "2001-07-04T15:30:45-05:00" -> "2001-07-04T15:30:45-05:00"
      • "2001-07-04T15:30:45Z" -> "2001-07-04T15:30:45Z"
      • "2001-07-04T15:30:45.1Z" -> "2001-07-04T15:30:45.1Z"
      • "2001-07-04T15:30Z" -> "2001-07-04T15:30:00Z"
      • "2001-07-04T15:30" -> "2001-07-04T15:30:00"
      • "2001-07-04" ->
      • "2001-07-04T00:00:00"
      • "2001-07" -> "2001-07-01T00:00:00"
      • "2001" -> "2001-01-01T00:00:00"
      Parameters:
      in - The string to pad.
      Returns:
      The padded string.
    • getFormatter

      public static DateTimeFormatter getFormatter(String pattern)
      Returns a DateTimeFormatter using either a pattern or predefined pattern name.
      Parameters:
      pattern - The pattern (e.g. "yyyy-MM-dd") or pattern name (e.g. "ISO_INSTANT").
      Returns:
      The formatter.