使用Joda时间显示一个数字的相对时间[Java]

时间:2015-01-16 09:43:42

标签: java jodatime duration period

所以我使用Joda time并希望它只显示一个数字(我知道如何处理Joda中的复数,最后只是一个简单的例子)。

例:
2 years 2 months 2 weeks ago 应该只显示 2 years ago
2 days 2 hours 2 minutes ago 应该只显示 2 days ago

我正在寻找一些方法来短接附加数字,当它获得一个非零数字时。我已经搜索并找到了其他框架的示例,但我们已经在使用Joda时间,如果可能的话,我不想拉其他依赖项。

我现在的代码如下:

protected final String toRelativeTime(final DateTime dateTime) {
    DateTime now = new DateTime();
    Period period = new Period(dateTime, now);

    PeriodFormatter formatter = new PeriodFormatterBuilder()
            .appendYears().appendSuffix(" years")
            .appendMonths().appendSuffix(" months")
            .appendWeeks().appendSuffix(" weeks")
            .appendDays().appendSuffix(" days")
            .appendHours().appendSuffix(" hours")
            .appendMinutes().appendSuffix(" minutes")
            .appendSeconds().appendSuffix(" seconds")
            .printZeroNever()
            .toFormatter();
    return formatter.print(period);
}

2 个答案:

答案 0 :(得分:1)

以下功能完全符合您的要求。

protected final String toRelativeTime(final DateTime dateTime) {
    long nowLngTime = System.currentTimeMillis();
    DateTime now = new DateTime();
    long difference = Math.abs(dateTime.getTime() - nowLngTime);
    Period period = new Period(dateTime, now);
    PeriodFormatterBuilder formatterBuilder = new PeriodFormatterBuilder();
    if (difference > DateUtils.YEAR_IN_MILLIS) {
        formatterBuilder.appendYears().appendSuffix(" year");
    } else if (difference > DateUtils.DAY_IN_MILLIS * 30) {
        formatterBuilder.appendMonths().appendSuffix(" month");
    } else if (difference > DateUtils.WEEK_IN_MILLIS) {
        formatterBuilder.appendWeeks().appendSuffix(" week");
    } else if (difference > DateUtils.DAY_IN_MILLIS) {
        formatterBuilder.appendDays().appendSuffix(" day");
    } else if (difference > DateUtils.HOUR_IN_MILLIS) {
        formatterBuilder.appendHours().appendSuffix(" hour");
    } else if (difference > DateUtils.MINUTE_IN_MILLIS) {
        formatterBuilder.appendMinutes().appendSuffix(" minute");
    } else if (difference > DateUtils.SECOND_IN_MILLIS) {
        formatterBuilder.appendSeconds().appendSuffix(" second");
    }
    String ends = formatterBuilder.printZeroNever().toFormatter().print(period);
    String plural = ends.startsWith("1 ")?"":"s";
    return plural;
}

答案 1 :(得分:1)

我最终做了这个

private final String toRelativeTime(LocalDateTime startTime, LocalDateTime endTime) {
    final String sufix = " ago";
    final Long seconds = ChronoUnit.SECONDS.between(startTime, endTime);
    final Long minutes = ChronoUnit.MINUTES.between(startTime, endTime);
    final Long hours = ChronoUnit.HOURS.between(startTime, endTime);
    final Long days = ChronoUnit.DAYS.between(startTime, endTime);
    final Long weeks = ChronoUnit.WEEKS.between(startTime, endTime);
    final Long months = ChronoUnit.MONTHS.between(startTime, endTime);
    final Long years = ChronoUnit.YEARS.between(startTime, endTime);

    if(years > 0) {
        return years.toString() + " " + checkPluralisation(years, "year", "years") + sufix;
    } else if(months > 0) {
        return months.toString() + " " + checkPluralisation(months, "month", "months") + sufix;
    }else if(weeks > 0) {
        return weeks.toString() + " " + checkPluralisation(weeks, "week", "weeks") + sufix;
    }else if(days > 0) {
        return days.toString() + " " + checkPluralisation(days, "day", "days") + sufix;
    }else if(hours > 0) {
        return hours.toString() + " " + checkPluralisation(hours, "hour", "hours") + sufix;
    }else if(minutes > 0) {
        return minutes.toString() + " " + checkPluralisation(minutes, "minute", "minutes") + sufix;
    } else {
        return seconds.toString() + " " + checkPluralisation(seconds, "second", "seconds") + sufix;
    }
}