I'm trying to build a DateTimeFormatter that can accepts offset with colon or offset without colon.
Is there a way to pass this test :
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[X]");
dateTimeFormatter.parse("2015-01-28T10:21:44+0100"); // OK
dateTimeFormatter.parse("2015-01-28T10:21:44+01:00"); // KO
If we use New York-based date-time (UTC -4), we can use “z” pattern-letter for time-zone name: String newYorkDateTimePattern = "dd. MM. yyyy HH:mm z"; DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.
Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.
ISO_DATE_TIME. The ISO-like date-time formatter that formats or parses a date-time with the offset and zone if available, such as '2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'. static DateTimeFormatter.
DateTimeFormatter fmt = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss"); System. out. println(ldt.
This: yyyy-MM-dd'T'HH:mm:ss[XXX][X]
seems to work.
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss[XXX][X]"); dateTimeFormatter.parse("2015-01-28T10:21:44+0100"); dateTimeFormatter.parse("2015-01-28T10:21:44+01:00");
Here is the ultimate pattern matching for pretty everything looking like an ISO string date !
"[yyyyMMdd][yyyy-MM-dd][yyyy-DDD]['T'[HHmmss][HHmm][HH:mm:ss][HH:mm][.SSSSSSSSS][.SSSSSS][.SSS][.SS][.S]][OOOO][O][z][XXXXX][XXXX]['['VV']']"
It works for the following list of tests except for the two in comments but I still don't know why...
ZonedDateTime dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+02:00:00[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+02:00[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+020000[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+0200[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+0000[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993Z[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993GMT+1[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993UTC[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993PST[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+02:00[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+020000[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+0200[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+0000[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08Z[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08GMT+1[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08UTC[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08PST[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08[Europe/Paris]"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+02:00:00"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+02:00"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+020000"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+0200"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993+0000"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993Z"); //dt = DateTimeUtils.parse("2016-10-27T16:36:08.993GMT+1"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993UTC"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993PST"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.993"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.000993"); dt = DateTimeUtils.parse("2016-10-27T16:36:08.000000993"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+02:00:00"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+02:00"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+020000"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+0200"); dt = DateTimeUtils.parse("2016-10-27T16:36:08+0000"); dt = DateTimeUtils.parse("2016-10-27T16:36:08Z"); //dt = DateTimeUtils.parse("2016-10-27T16:36:08GMT+1"); dt = DateTimeUtils.parse("2016-10-27T16:36:08UTC"); dt = DateTimeUtils.parse("2016-10-27T16:36:08PST"); dt = DateTimeUtils.parse("2016-10-27T16:36:08"); dt = DateTimeUtils.parse("2016-100T16:36:08Z"); dt = DateTimeUtils.parse("2016-100T16:36.1Z"); dt = DateTimeUtils.parse("2016-10-27"); dt = DateTimeUtils.parse("20161223T163608"); dt = DateTimeUtils.parse("20161223T1636");
And the parse method itself :
public static ZonedDateTime parse(CharSequence text) { TemporalAccessor temporalAccessor = LOOSE_ISO_DATE_TIME_ZONE_PARSER.parseBest(text, ZonedDateTime::from, LocalDateTime::from, LocalDate::from); if (temporalAccessor instanceof ZonedDateTime) { return ((ZonedDateTime) temporalAccessor); } if (temporalAccessor instanceof LocalDateTime) { return ((LocalDateTime) temporalAccessor).atZone(ZoneId.systemDefault()); } return ((LocalDate) temporalAccessor).atStartOfDay(ZoneId.systemDefault()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With