JodaTime显示持续数周

时间:2017-05-06 12:59:38

标签: java datetime jodatime duration

我试图以下列格式显示两个DateTime之间的时间差:

? years, ? months, ? weeks, ? days, ? hours, ? minutes, ? seconds and ? milliseconds

我显示它几乎就是这样,但它没有显示周。我尝试过使用PeriodType.standard(),但这省略了几个月和几年。有可能这样做吗?

以下是我用来实现当前结果的代码:

private String getPeriodBetween(DateTime from, DateTime to, boolean showMilliseconds) {
    Period period;

    if (from.isAfter(to)) {
        period = new Period(to, from);
    } else {
        period = new Period(from, to);
    }

    PeriodFormatterBuilder builder = new PeriodFormatterBuilder().appendYears()
        .appendSuffix(" year, ", " years, ")
        .appendMonths()
        .appendSuffix(" month, ", " months, ")
        .appendWeeks()
        .appendSuffix(" week, ", " weeks, ")
        .appendDays()
        .appendSuffix(" day, ", " days, ")
        .appendHours()
        .appendSuffix(" hour, ", " hours, ")
        .appendMinutes()
        .appendSuffix(" minute, ", " minutes, ")
        .appendSeconds()
        .appendSuffix(" second, ", " seconds, ");

    if (showMilliseconds) {
        builder = builder.appendMillis().appendSuffix(" millisecond", " milliseconds");
    }

    return builder.toFormatter()
        .print(period.normalizedStandard(PeriodType.yearMonthDayTime()))
        .replaceAll("[,]\\s*$", "")
        .replaceAll("(?:,\\s*)([^,]+)$", " and $1")
        .replaceAll("^(?![\\s\\S])", "No difference");
}

如果我使用PeriodType.forFields();并指定包含所有必填字段的DurationFieldType数组,则会忽略年份和月份。

1 个答案:

答案 0 :(得分:0)

搞清楚了。问题是我最初使用Duration并将其转换为Period。我曾尝试使用以下代码来修复它,但它并没有给我我预期的结果。在我将其更改为Period之后,我可以使用Period.forFields();指定我需要的所有字段,接受以下内容:

private static final DurationFieldType[] DURATION_FIELD_TYPES = {
    DurationFieldType.years(),
    DurationFieldType.months(),
    DurationFieldType.weeks(),
    DurationFieldType.days(),
    DurationFieldType.hours(),
    DurationFieldType.minutes(),
    DurationFieldType.seconds(),
    DurationFieldType.millis(),
};

<击>

没关系,您甚至不需要标准化标准,因为它默认包含所有字段。现在我考虑一下,这是有道理的。这是我完整的工作代码供参考:

private String getDifference(DateTime from, DateTime to, boolean showMilliseconds) {
    Period period;

    if (from.isAfter(to)) {
        period = new Period(to, from);
    } else {
        period = new Period(from, to);
    }

    PeriodFormatterBuilder builder = new PeriodFormatterBuilder()
        .appendYears()
        .appendSuffix(" year, ", " years, ")
        .appendMonths()
        .appendSuffix(" month, ", " months, ")
        .appendWeeks()
        .appendSuffix(" week, ", " weeks, ")
        .appendDays()
        .appendSuffix(" day, ", " days, ")
        .appendHours()
        .appendSuffix(" hour, ", " hours, ")
        .appendMinutes()
        .appendSuffix(" minute, ", " minutes, ")
        .appendSeconds()
        .appendSuffix(" second, ", " seconds, ");

    if (showMilliseconds) {
        builder = builder.appendMillis().appendSuffix(" millisecond", " milliseconds");
    }

    return builder
        .toFormatter()
        .print(period)
        .replaceAll("[,]\\s*$", "")
        .replaceAll("(?:,\\s*)([^,]+)$", " and $1")
        .replaceAll("^(?![\\s\\S])", "No difference");
}