Class Iso8601Utils
Provides the built-in serialization format for Temporal, Calendar,
Date, XMLGregorianCalendar, and Duration objects.
See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringFormats a date/time or Duration value to its ISO 8601 string representation.static StringformatAsDate(Object value, ClassMeta<?> type, TimeZone timeZone) Formats a date/time value as an ISO date (date-only, for OpenAPI 'date' format).static StringformatAsDateTime(Object value, ClassMeta<?> type, TimeZone timeZone) Formats a date/time value as an ISO date-time (for OpenAPI 'date-time' format).static StringformatCalendar(Object value, ClassMeta<?> sourceType, CalendarFormat format, TimeZone timeZone) Formats aCalendarorXMLGregorianCalendarvalue using the supplied wire format.static StringformatDate(Date value, ClassMeta<?> sourceType, DateFormat format, TimeZone timeZone) Formats aDatevalue using the supplied wire format.static StringformatDuration(Duration value, DurationFormat format) Formats aDurationvalue using the supplied wire format.static StringformatPeriod(Period value, PeriodFormat format) Formats aPeriodvalue using the supplied wire format.static StringformatTemporal(TemporalAccessor value, ClassMeta<?> sourceType, TemporalFormat format, TimeZone timeZone) Formats aTemporalAccessorvalue using the supplied wire format.static <T> TfromEpochMillis(long epochMillis, ClassMeta<T> targetType, TimeZone timeZone) Converts epoch milliseconds to the target date/time type.static <T> TParses an ISO 8601 string into the specified date/time target type.static <T> TparseCalendar(String iso8601, ClassMeta<T> targetType, CalendarFormat formatHint, TimeZone timeZone) Parses an ISO 8601 wire value into aCalendarorXMLGregorianCalendar.static <T> TparseDate(String iso8601, ClassMeta<T> targetType, DateFormat formatHint, TimeZone timeZone) Parses an ISO 8601 wire value into aDate.static DurationparseDuration(String iso8601, DurationFormat formatHint) Parses an ISO 8601 / numeric wire value into aDuration.static PeriodparsePeriod(String value, PeriodFormat formatHint) Parses an ISO 8601 / numeric wire value into aPeriod.static <T> TparseTemporal(String iso8601, ClassMeta<T> targetType, TemporalFormat formatHint, TimeZone timeZone) Parses an ISO 8601 wire value into the specifiedTemporalsubtype.
-
Method Details
-
format
Formats a date/time or Duration value to its ISO 8601 string representation.Convenience dispatcher that uses default per-type wire formats; callers that need format-aware output should use the per-type helpers directly:
formatDate(java.util.Date, org.apache.juneau.marshall.ClassMeta<?>, org.apache.juneau.marshall.DateFormat, java.util.TimeZone),formatCalendar(java.lang.Object, org.apache.juneau.marshall.ClassMeta<?>, org.apache.juneau.marshall.CalendarFormat, java.util.TimeZone),formatTemporal(java.time.temporal.TemporalAccessor, org.apache.juneau.marshall.ClassMeta<?>, org.apache.juneau.marshall.TemporalFormat, java.util.TimeZone),formatDuration(java.time.Duration, org.apache.juneau.marshall.DurationFormat),formatPeriod(java.time.Period, org.apache.juneau.marshall.PeriodFormat).- Parameters:
value- The value to format.null returnsnull (consistent with the per-type helpers).type- The class metadata for the value (used for selecting the appropriate formatter for Temporal types).timeZone- The session time zone (used when the value lacks zone info).- Returns:
- The ISO 8601 string representation, or
null ifvalueisnull .
-
formatDuration
Formats aDurationvalue using the supplied wire format.- Parameters:
value- The value to format.null returnsnull .format- The duration wire format. May benull (defaults toDurationFormat.ISO_8601_WITH_DAYS).- Returns:
- The formatted value, or
null ifvalueisnull .
-
formatPeriod
Formats aPeriodvalue using the supplied wire format.- Parameters:
value- The value to format.null returnsnull .format- The period wire format. May benull (defaults toPeriodFormat.ISO_8601).- Returns:
- The formatted value, or
null ifvalueisnull .
-
formatDate
public static String formatDate(Date value, ClassMeta<?> sourceType, DateFormat format, TimeZone timeZone) Formats aDatevalue using the supplied wire format.java.sql.Date,java.sql.Time, andjava.sql.Timestampsubclasses all dispatch throughDateFormat.format(Date, ZoneId)just likeDate.- Parameters:
value- The value to format.null returnsnull .sourceType- The class metadata for the value (currently unused, kept for symmetry with the parse helper).format- The date wire format. May benull (defaults toDateFormat.ISO_LOCAL_DATE_TIME).timeZone- The session time zone (used as the default zone).- Returns:
- The formatted value, or
null ifvalueisnull .
-
formatCalendar
public static String formatCalendar(Object value, ClassMeta<?> sourceType, CalendarFormat format, TimeZone timeZone) Formats aCalendarorXMLGregorianCalendarvalue using the supplied wire format.XMLGregorianCalendarvalues always emitXMLGregorianCalendar.toXMLFormat()regardless of the configuredformat— mirroring the parse-side rule and the historical wire output.Calendar(includingGregorianCalendar) values are formatted using the suppliedCalendarFormat.- Parameters:
value- The value to format. Must be aCalendarorXMLGregorianCalendar.null returnsnull .sourceType- The class metadata for the value (currently unused, kept for symmetry with the parse helper).format- The calendar wire format. May benull (defaults toCalendarFormat.ISO_OFFSET_DATE_TIME).timeZone- The session time zone (used as the default zone when the value lacks zone info).- Returns:
- The formatted value, or
null ifvalueisnull .
-
formatTemporal
public static String formatTemporal(TemporalAccessor value, ClassMeta<?> sourceType, TemporalFormat format, TimeZone timeZone) Formats aTemporalAccessorvalue using the supplied wire format.- Parameters:
value- The value to format. Must be aTemporalsubtype.null returnsnull .sourceType- The class metadata for the value (currently unused, kept for symmetry with the parse helper).format- The temporal wire format. May benull (defaults toTemporalFormat.DEFAULT).timeZone- The session time zone (used as the default zone when the value lacks zone info).- Returns:
- The formatted value, or
null ifvalueisnull .
-
formatAsDate
Formats a date/time value as an ISO date (date-only, for OpenAPI 'date' format).- Parameters:
value- The value to format.null returnsnull .type- The class metadata.timeZone- The session time zone.- Returns:
- The ISO date string, or
null ifvalueisnull .
-
formatAsDateTime
Formats a date/time value as an ISO date-time (for OpenAPI 'date-time' format).- Parameters:
value- The value to format.null returnsnull .type- The class metadata.timeZone- The session time zone.- Returns:
- The ISO date-time string, or
null ifvalueisnull .
-
parse
Parses an ISO 8601 string into the specified date/time target type.Dispatches to the appropriate per-type parser based on the target's
ClassMetacategory:parseDate,parseCalendar,parseTemporal,parseDuration, orparsePeriod— each using its default wire format.- Type Parameters:
T- The target type.- Parameters:
iso8601- The ISO 8601 string to parse.null returnsnull .targetType- The target class metadata.timeZone- The session time zone (used for types that need a default zone).- Returns:
- The parsed object, or
null ifiso8601isnull or the target type is not a recognized date/time/duration/period type.
-
parseDuration
Parses an ISO 8601 / numeric wire value into aDuration.Handles ISO 8601 forms (
"PT1H","-PT30M","PT-6H","PT1.5S", …) as well as the bare-number wire forms produced byDurationFormat.MILLIS,DurationFormat.SECONDS,DurationFormat.NANOS, andDurationFormat.HOCON(e.g."500ms","1.5s").The
formatHintdisambiguates bare integer strings that could be milliseconds, nanos, or seconds — for all other numeric or ISO forms the input is self-describing and the hint is ignored.- Parameters:
iso8601- The wire value to parse.null or empty returnsnull .formatHint- The duration wire format used to disambiguate bare integer inputs. May benull (defaults toDurationFormat.MILLIS).- Returns:
- The parsed
Duration, ornull ifiso8601isnull or empty.
-
parsePeriod
Parses an ISO 8601 / numeric wire value into aPeriod.Handles standard ISO 8601 period strings (
"P1Y2M3D", …) as well as the bare-integer wire form produced byPeriodFormat.DAYS.- Parameters:
value- The wire value to parse.null or empty returnsnull .formatHint- The period wire format used to interpret bare integers. May benull (defaults toPeriodFormat.DAYS).- Returns:
- The parsed
Period, ornull ifvalueisnull or empty.
-
parseCalendar
public static <T> T parseCalendar(String iso8601, ClassMeta<T> targetType, CalendarFormat formatHint, TimeZone timeZone) Parses an ISO 8601 wire value into aCalendarorXMLGregorianCalendar.Dispatches by target type:
XMLGregorianCalendartargets always useDatatypeFactory.newXMLGregorianCalendar(String)and ignoreformatHint.Calendar/GregorianCalendartargets honorformatHintwhen set to anything other thanCalendarFormat.NOT_SETorCalendarFormat.ISO_OFFSET_DATE_TIME; the default path uses content-based formatter autodetection.
- Type Parameters:
T- The target type.- Parameters:
iso8601- The wire value to parse.null returnsnull .targetType- The target class metadata. Must represent aCalendarorXMLGregorianCalendar.formatHint- The calendar wire format. May benull (treated asCalendarFormat.NOT_SET).timeZone- The session time zone (used as the default zone when the wire value lacks zone info).- Returns:
- The parsed value, or
null ifiso8601isnull or the target is not a recognized calendar type.
-
parseDate
public static <T> T parseDate(String iso8601, ClassMeta<T> targetType, DateFormat formatHint, TimeZone timeZone) Parses an ISO 8601 wire value into aDate.Honors
formatHintwhen set to anything other thanDateFormat.NOT_SETorDateFormat.ISO_LOCAL_DATE_TIME; the default path uses content-based formatter autodetection.Only direct
Datetargets are produced —java.sql.*subclasses fall through tonull , matching the original dispatch.- Type Parameters:
T- The target type.- Parameters:
iso8601- The wire value to parse.null returnsnull .targetType- The target class metadata. Must representDate.formatHint- The date wire format. May benull (treated asDateFormat.NOT_SET).timeZone- The session time zone (used as the default zone when the wire value lacks zone info).- Returns:
- The parsed
Date, ornull ifiso8601isnull or the target is not exactlyDate.
-
parseTemporal
public static <T> T parseTemporal(String iso8601, ClassMeta<T> targetType, TemporalFormat formatHint, TimeZone timeZone) Parses an ISO 8601 wire value into the specifiedTemporalsubtype.Honors
formatHintwhen set to anything other thanTemporalFormat.NOT_SETorTemporalFormat.DEFAULT; the default path uses the per-subtype default formatter (e.g.DateTimeFormatter.ISO_INSTANTforInstant,DateTimeFormatter.ISO_LOCAL_DATEforLocalDate, …).- Type Parameters:
T- The target temporal subtype.- Parameters:
iso8601- The wire value to parse.null returnsnull .targetType- The target class metadata. Must represent aTemporalsubtype.formatHint- The temporal wire format. May benull (treated asTemporalFormat.NOT_SET).timeZone- The session time zone (used as the default zone when the wire value lacks zone info).- Returns:
- The parsed value, or
null ifiso8601isnull or the target is not aTemporalsubtype.
-
fromEpochMillis
Converts epoch milliseconds to the target date/time type.- Type Parameters:
T- The target type.- Parameters:
epochMillis- The epoch milliseconds value.targetType- The target class metadata.timeZone- The session time zone.- Returns:
- The converted date/time object, or
null if the target type is not a recognized date/time type.
-