joda-time ISODateTimeFormat.dateTime() 行为不端

joda-time ISODateTimeFormat.dateTime() misbehaving

ISODateTimeFormat.dateTime() 应该是宽容和灵活的。例如。毫秒是可选的,日期是可选的,时区是可选的,时区可以是Z+/- HH[:MM]

但事实并非如此。我错过了什么?

使用版本 joda-time:2.9.9(也尝试了 2.10.8,结果相同)

import org.joda.time.format.ISODateTimeFormat;
import java.util.Arrays;

public static void main(String[] args) {
    Arrays.asList("2020-11-22T01:59:59.001+00:00", "2020-11-22T01:59:59.001Z", "2020-11-22T01:59:59+00:00",
        "2020-11-22T01:59:59Z", "01:59:59").forEach(s -> {
        try {
            System.out.println(String.format("%-30s -- %s", s, ISODateTimeFormat.dateTime().parseDateTime(s)));
        } catch (Exception e) {
            System.out.println(String.format("%-30s -- %s", s, e.getMessage()));
        }
    });
}

输出:

2020-11-22T01:59:59.001+00:00  -- 2020-11-22T01:59:59.001Z
2020-11-22T01:59:59.001Z       -- 2020-11-22T01:59:59.001Z
2020-11-22T01:59:59+00:00      -- Invalid format: "2020-11-22T01:59:59+00:00" is malformed at "+00:00"
2020-11-22T01:59:59Z           -- Invalid format: "2020-11-22T01:59:59Z" is malformed at "Z"
01:59:59.000                   -- Invalid format: "01:59:59.000" is malformed at ":59:59.000"

javadoc:

public static DateTimeFormatter dateTimeParser()

Returns a generic ISO datetime parser which parses either a date or a time or both.

The returned formatter can only be used for parsing, printing is unsupported.

The parser is strict by default, thus time string 24:00 cannot be parsed.

It accepts formats described by the following syntax:

 datetime          = time | date-opt-time
 time              = 'T' time-element [offset]
 date-opt-time     = date-element ['T' [time-element] [offset]]
 date-element      = std-date-element | ord-date-element | week-date-element
 std-date-element  = yyyy ['-' MM ['-' dd]]
 ord-date-element  = yyyy ['-' DDD]
 week-date-element = xxxx '-W' ww ['-' e]
 time-element      = HH [minute-element] | [fraction]
 minute-element    = ':' mm [second-element] | [fraction]
 second-element    = ':' ss [fraction]
 fraction          = ('.' | ',') digit+
 offset            = 'Z' | (('+' | '-') HH [':' mm [':' ss [('.' | ',') SSS]]])
 

java.time

正如 Abra 在评论中所建议的那样,您可能需要遵循官方的 Joda-Time 推荐并继续使用 java.time,现代 Java 日期和时间 API。

以下不会为您提供 Joda-Time 解析器的全部灵活性,但它会解析您的五个示例字符串。而且您可以进一步调整它以满足您可能有的任何进一步需求。

    ZoneOffset defaultOffset = ZoneOffset.UTC; // Configurable
    LocalDate defaultDate = LocalDate.now(ZoneId.systemDefault());
    
    DateTimeFormatter flexibleFormatter = new DateTimeFormatterBuilder()
            .optionalStart()
            .append(DateTimeFormatter.ISO_LOCAL_DATE)
            .appendLiteral('T')
            .optionalEnd()
            .parseDefaulting(ChronoField.YEAR, defaultDate.getYear())
            .parseDefaulting(ChronoField.MONTH_OF_YEAR, defaultDate.getMonthValue())
            .parseDefaulting(ChronoField.DAY_OF_MONTH, defaultDate.getDayOfMonth())
            .append(DateTimeFormatter.ISO_LOCAL_TIME)
            .optionalStart()
            .appendOffsetId()
            .optionalEnd()
            .parseDefaulting(ChronoField.OFFSET_SECONDS, defaultOffset.getTotalSeconds())
            .toFormatter();

    Arrays.asList("2020-11-22T01:59:59.001+00:00", "2020-11-22T01:59:59.001Z", "2020-11-22T01:59:59+00:00",
            "2020-11-22T01:59:59Z", "01:59:59").forEach(s -> {
            try {
                System.out.println(String.format("%-30s -- %s", s, OffsetDateTime.parse(s, flexibleFormatter)));
            } catch (DateTimeParseException dtpe) {
                System.out.println(String.format("%-30s -- %s", s, dtpe.getMessage()));
            }
        });     

我运行刚刚在Europe/Copenhagen时区输出:

2020-11-22T01:59:59.001+00:00  -- 2020-11-22T01:59:59.001Z
2020-11-22T01:59:59.001Z       -- 2020-11-22T01:59:59.001Z
2020-11-22T01:59:59+00:00      -- 2020-11-22T01:59:59Z
2020-11-22T01:59:59Z           -- 2020-11-22T01:59:59Z
01:59:59                       -- 2020-11-19T01:59:59Z

来自 Joda-Time 主页:

Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

你的代码出了什么问题?

jensgram 已经在评论里说了:

You're linking to the docs for dateTimeParser but invoking dateTime()

链接