带有可选冒号分隔符的时区的 Java DateTimeFormatter?

2022-09-01 18:08:08

我正在尝试构建一个DateTimeFormatter,它可以接受冒号偏移量或不带冒号的偏移量。

有没有办法通过这个测试:

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

答案 1

这:似乎有效。yyyy-MM-dd'T'HH:mm:ss[XXX][X]

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");

答案 2

这是几乎所有看起来像ISO字符串日期的终极模式匹配!

"[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']']"

它适用于以下测试列表,除了评论中的两个,但我仍然不知道为什么...

    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");

解析方法本身:

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());
}