Class DateTimeFormats
Instant
and ZonedDateTime
parsing for all common
date time strings, without needing a DateTimeFormatter
:
Instant timestamp = DateTimeFormats.parseToInstant(timestampString);
ZonedDateTime dateTime = DateTimeFormats.parseZonedDateTime(dateTimeString);
For more flexible use cases, where you might want to reuse or conform to a format known at
compile-time, the DateTimeFormatter
can be inferred from an example date/time/datetime
string (similar to the golang time library style).
For example:
private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormats.formatOf("2023-12-09 10:00:00.12345 America/Los_Angeles");
private static final DateTimeFormatter USING_ZONE_OFFSET =
DateTimeFormats.formatOf("2023-12-09 10:00:00+08:00");
private static final DateTimeFormatter ISO_FORMATTER =
DateTimeFormats.formatOf("2023-12-09T10:00:00.12345[Europe/Paris]");
private static final DateTimeFormatter WITH_DAY_OF_WEEK =
DateTimeFormats.formatOf("2023/12/09 Sat 10:00+08:00");
Most ISO 8601 formats are supported, except BASIC_ISO_DATE, ISO_WEEK_DATE ('2012-W48-6') and ISO_ORDINAL_DATE ('2012-337'), which are rarely used.
For the date part of custom patterns, ambiguous examples like 10/12/2024
or
1/2/yyyy
are not supported. You should use unambiguous examples like 10/30/2024
(which results in "MM/dd/yyyy"} or 30/1/2024
(which results in "dd/M/yyyy}.
In addition, localized month names such as Jan
or March
are used, all natural orders
(year month day
, month day year
or day month year
) are supported.
For the time part of custom patterns, only HH:mm
, HH:mm:ss
and
HH:mm:ss.S
variants are supported (the S can be 1 to 9 digits). AM/PM and 12-hour numbers are
not supported. Though you can explicitly specify them together with placeholders (see below).
If the variant of the date time pattern you need exceeds the out-of-box support, you can
explicitly mix the DateTimeFormatter
specifiers with example placeholders
(between a pair of pointy brackets) to be translated.
s
For example the following code uses the dd
, MM
and yyyy
specifiers as
is but translates the Tue
and America/New_York
example snippets into E
and VV
specifiers respectively. It will then parse and format to datetime strings like
"Fri, 20 Oct 2023 10:30:59.123 Europe/Paris".
private static final DateTimeFormatter FORMATTER =
formatOf("<Tue>, dd MM yyyy HH:mm:ss.SSS <America/New_York>");
- Since:
- 7.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic DateTimeFormatter
Infers and returns theDateTimeFormatter
based onexample
.static LocalDate
parseLocalDate
(String dateString) ParsesdateString
asLocalDate
.static OffsetDateTime
parseOffsetDateTime
(String dateTimeString) ParsesdateTimeString
toOffsetDateTime
using heuristics in this class to infer theDateTimeFormatter
for common formats.static Instant
parseToInstant
(String dateTimeString) ParsesdateTimeString
toInstant
.static ZonedDateTime
parseZonedDateTime
(String dateTimeString) ParsesdateTimeString
toZonedDateTime
using heuristics in this class to infer theDateTimeFormatter
for common formats.
-
Method Details
-
formatOf
Infers and returns theDateTimeFormatter
based onexample
.- Throws:
DateTimeException
- ifexample
is invalid or the pattern isn't supported.
-
parseLocalDate
ParsesdateString
asLocalDate
.Acceptable formats include dates like "2024/04/11", "2024-04-11", "2024 April 11", "Apr 11 2024", "11 April 2024", "20240401", or even with "10/30/2024", "30/01/2024" etc. as long as it's not ambiguous.
Prefer to pre-construct a
DateTimeFormatter
usingformatOf(java.lang.String)
to get better performance and earlier error report in case the format cannot be inferred.- Throws:
DateTimeException
- ifdateTimeString
cannot be parsed toLocalDate
- Since:
- 8.0
-
parseToInstant
ParsesdateTimeString
toInstant
.dateTimeString
could be in the format ofDateTimeFormatter.ISO_INSTANT
, which is fromInstant.toString()
; or it could be any valid date time with zone name or zone offset.Prefer to pre-construct a
DateTimeFormatter
usingformatOf(java.lang.String)
to get better performance and earlier error report in case the format cannot be inferred.- Parameters:
dateTimeString
- can be the result ofInstant.toString()
, or any other valid date time with either zone name or UTC offset.- Throws:
DateTimeException
- ifdateTimeString
cannot be parsed asInstant
- Since:
- 8.0
-
parseZonedDateTime
ParsesdateTimeString
toZonedDateTime
using heuristics in this class to infer theDateTimeFormatter
for common formats.Prefer to pre-construct a
DateTimeFormatter
usingformatOf(java.lang.String)
to get better performance and earlier error report in case the format cannot be inferred.- Parameters:
dateTimeString
- must be a string with valid date, time, and zone name or UTC offset- Throws:
DateTimeException
- ifdateTimeString
cannot be parsed asZonedDateTime
- Since:
- 8.0
-
parseOffsetDateTime
ParsesdateTimeString
toOffsetDateTime
using heuristics in this class to infer theDateTimeFormatter
for common formats.Prefer to pre-construct a
DateTimeFormatter
usingformatOf(java.lang.String)
to get better performance and earlier error report in case the format cannot be inferred.- Parameters:
dateTimeString
- must be a string with valid date, time, and UTC offset (cannot be zone name)- Throws:
DateTimeException
- ifdateTimeString
cannot be parsed asOffsetDateTime
- Since:
- 8.0
-