Package com.ibm.cloud.sdk.core.util
Class DateUtils
- java.lang.Object
-
- com.ibm.cloud.sdk.core.util.DateUtils
-
public class DateUtils extends java.lang.ObjectThis class contains utilities for formatting and parsingDateinstances 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.DateTimeFormatterrfc3339DateTimeFmtstatic java.time.format.DateTimeFormatterrfc3339FullDateFmt
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringformatAsDate(java.util.Date d)Formats the specifiedDateinstance as an RFC 3339 "full-date" value (yyyy-MM-dd).
TheDateinstance is assumed to represent the start of the specified day in UTC time.static java.lang.StringformatAsDateTime(java.util.Date d)Formats the specifiedDateinstance as an RFC 3339 "date-time" value (a string of the form "yyyy-MM-dd'T'HH:mm:ss.SSSZ").
TheDateinstance represents a moment in time (the number of milliseconds since epoch time in UTC).static java.util.DateparseAsDate(java.lang.String s)Parses the specified string (assumed to be of the form "yyyy-MM-dd") into aDateinstance.static java.util.DateparseAsDateTime(java.lang.String dateAsString)Parses the specified string into aDateinstance.
-
-
-
Method Detail
-
formatAsDate
public static java.lang.String formatAsDate(java.util.Date d)
Formats the specifiedDateinstance as an RFC 3339 "full-date" value (yyyy-MM-dd).
TheDateinstance 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- theDateinstance 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 specifiedDateinstance as an RFC 3339 "date-time" value (a string of the form "yyyy-MM-dd'T'HH:mm:ss.SSSZ").
TheDateinstance represents a moment in time (the number of milliseconds since epoch time in UTC).- Parameters:
d- theDateinstance 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 aDateinstance. Specifically, the string is parsed into aDateinstance that represents the start of the specified day in UTC time. This is aligned with theformatAsDate(java.util.Date)method which formats theDateinstance using "yyyy-MM-dd" without any adjustment for timezone.- Parameters:
s- the string to be parsed- Returns:
- the resulting
Datevalue - 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 aDateinstance. The supported formats are:- 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 - 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 - 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 - 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 - "Dialog" date-time: yyyy-MM-dd HH:mm:ss
Examples: 2020-01-01 12:00:00 - "Alchemy" date-time: yyyyMMdd'T'HHmmss
Examples: 20200101T120000 - A "full-date": yyyy-MM-dd
Examples: 2020-01-01 - A raw time value (# of milliseconds since epoch time in UTC)
Examples: 1584024732866
- Parameters:
dateAsString- the string to be parsed- Returns:
- the resulting
Dateinstance - Throws:
java.time.DateTimeException- if an error occurs during parsing
- RFC 3339 "date-time": yyyy-MM-dd'T'HH:mm:ss[.nnnnnnnnn]X (optional ms, tz is 'Z' or +/-hh:mm)
-
-