Class DateUtils


  • public class DateUtils
    extends java.lang.Object
    This class contains utilities for formatting and parsing Date instances as OpenAPI "date" or "date-time" values.

    References:
    API Handbook:
    • https://cloud.ibm.com/docs/api-handbook?topic=api-handbook-types#date
    • https://cloud.ibm.com/docs/api-handbook?topic=api-handbook-types#date-time
    OpenAPI Specification:
    • https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types
    RFC 3339:
    • https://tools.ietf.org/html/rfc3339#page-6
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.time.format.DateTimeFormatter rfc3339DateTimeFmt  
      static java.time.format.DateTimeFormatter rfc3339FullDateFmt  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String formatAsDate​(java.util.Date d)
      Formats the specified Date instance as an RFC 3339 "full-date" value (yyyy-MM-dd).
      The Date instance is assumed to represent the start of the specified day in UTC time.
      static java.lang.String formatAsDateTime​(java.util.Date d)
      Formats the specified Date instance as an RFC 3339 "date-time" value (a string of the form "yyyy-MM-dd'T'HH:mm:ss.SSSZ").
      The Date instance represents a moment in time (the number of milliseconds since epoch time in UTC).
      static java.util.Date parseAsDate​(java.lang.String s)
      Parses the specified string (assumed to be of the form "yyyy-MM-dd") into a Date instance.
      static java.util.Date parseAsDateTime​(java.lang.String dateAsString)
      Parses the specified string into a Date instance.
      • Methods inherited from class java.lang.Object

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

      • rfc3339FullDateFmt

        public static final java.time.format.DateTimeFormatter rfc3339FullDateFmt
      • rfc3339DateTimeFmt

        public static final java.time.format.DateTimeFormatter rfc3339DateTimeFmt
    • Method Detail

      • formatAsDate

        public static java.lang.String formatAsDate​(java.util.Date d)
        Formats the specified Date instance as an RFC 3339 "full-date" value (yyyy-MM-dd).
        The Date instance is assumed to represent the start of the specified day in UTC time. No adjustment for timezone is performed while formatting the value.
        Parameters:
        d - the Date instance to be formatted
        Returns:
        a string containing the RFC3339 "full-date" representation of "d" (example: 2020-01-01).
        Throws:
        java.time.DateTimeException - if an error occurs during formatting
      • formatAsDateTime

        public static java.lang.String formatAsDateTime​(java.util.Date d)
        Formats the specified Date instance as an RFC 3339 "date-time" value (a string of the form "yyyy-MM-dd'T'HH:mm:ss.SSSZ").
        The Date instance represents a moment in time (the number of milliseconds since epoch time in UTC).
        Parameters:
        d - the Date instance to be formatted
        Returns:
        a string containing the UTC representation of "d" (example: 2020-01-01T12:00:00.000Z).
        Throws:
        java.time.DateTimeException - if an error occurs during formatting
      • parseAsDate

        public static java.util.Date parseAsDate​(java.lang.String s)
        Parses the specified string (assumed to be of the form "yyyy-MM-dd") into a Date instance. Specifically, the string is parsed into a Date instance that represents the start of the specified day in UTC time. This is aligned with the formatAsDate(java.util.Date) method which formats the Date instance using "yyyy-MM-dd" without any adjustment for timezone.
        Parameters:
        s - the string to be parsed
        Returns:
        the resulting Date value
        Throws:
        java.time.DateTimeException - if an error occurs during parsing
      • parseAsDateTime

        public static java.util.Date parseAsDateTime​(java.lang.String dateAsString)
        Parses the specified string into a Date instance. The supported formats are:
        1. RFC 3339 "date-time": yyyy-MM-dd'T'HH:mm:ss[.nnnnnnnnn]X (optional ms, tz is 'Z' or +/-hh:mm)
          Examples: 2020-01-01T12:00:00.000Z, 2020-01-01T07:00:00-05:00
        2. Same as above, but with no colon in tz-offset (e.g. -0300)
          Examples: 2020-01-01T09:00:00.000-0300, 2020-01-01T16:00:00+0400
        3. Same as above, but with a 2-digit tz-offset (e.g. -03)
          Examples: 2020-01-01T09:00:00.000-03, 2020-01-01T16:00:00+04
        4. UTC "date-time" with no tz: yyyy-MM-dd'T'HH:mm:ss[.nnnnnnnnn] (optional fractional seconds)
          Examples: 2020-01-01T12:00:00.000, 2020-01-01T12:00:00
        5. "Dialog" date-time: yyyy-MM-dd HH:mm:ss
          Examples: 2020-01-01 12:00:00
        6. "Alchemy" date-time: yyyyMMdd'T'HHmmss
          Examples: 20200101T120000
        7. A "full-date": yyyy-MM-dd
          Examples: 2020-01-01
        8. A raw time value (# of milliseconds since epoch time in UTC)
          Examples: 1584024732866
        Parameters:
        dateAsString - the string to be parsed
        Returns:
        the resulting Date instance
        Throws:
        java.time.DateTimeException - if an error occurs during parsing