java 如何比较两个 Joda 时间段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6467950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:59:44  来源:igfitidea点击:

How to compare two Joda time Periods

javajodatime

提问by Persimmonium

It does not seem straighforward.

看起来并不直截了当。

I am trying this:

我正在尝试这个:

@Override
public int compare(Period o1, Period o2) {
    return o1.toStandardDays().getDays() > o2.toStandardDays().getDays() ? -1 : (o1.toStandardDays().getDays() == o2.toStandardDays().getDays() ? 0 : 1);
}

But I get this exception:

但我得到这个例外:

java.lang.UnsupportedOperationException: Cannot convert to Days as this period contains months and months vary in length
    at org.joda.time.Period.checkYearsAndMonths(Period.java:1455)
    at org.joda.time.Period.toStandardDays(Period.java:1314)

I hoped Peroidwould have an isLongerThan(Period p)method.

我希望Peroid有一个isLongerThan(Period p)方法。

采纳答案by Matt Ball

From the Joda Documentation:

乔达文档

To compare the actual duration of two periods, convert both to durations using toDuration, an operation that emphasises that the result may differ according to the date you choose.

要比较两个期间的实际持续时间,请使用 将两者转换为持续时间toDuration,该操作强调结果可能因您选择的日期而异。

The two toDurationmethods are BasePeriod#toDurationTo(ReadableInstant)and BasePeriod#toDurationFrom(ReadableInstant). This means that you mustchoose either a start or end instant of this period in order to be able to compute its duration.

这两种toDuration方法是BasePeriod#toDurationTo(ReadableInstant)BasePeriod#toDurationFrom(ReadableInstant)。这意味着您必须选择该时间段的开始或结束时刻才能计算其持续时间。

If that is a problem for you, then you might want to directly use Durationinstead of Period.

如果这对您来说是个问题,那么您可能想直接使用Duration而不是Period.

回答by THelper

As Matt Ball also explained in his answer, to compare 2 periods you need to convert them to a duration first. Durations are relative to a certain point in time, so you need to do something like this:

正如马特鲍尔在他的回答中也解释的那样,要比较 2 个时期,您需要先将它们转换为持续时间。持续时间是相对于某个时间点的,所以你需要做这样的事情:

public static boolean isLonger(Period p1, Period p2) {
   Instant now = Instant.now();
   Duration d1 = p1.toDurationTo(now);
   Duration d2 = p2.toDurationTo(now);
   return d1.isLongerThan(d2);
}

public static boolean isShorter(Period p1, Period p2) {
   Instant now = Instant.now();
   Duration d1 = p1.toDurationTo(now);
   Duration d2 = p2.toDurationTo(now);
   return d1.isShorterThan(d2);
}

回答by Alex Spurling

I wrote a method that should be able to compare two Periods to the nearest day (I didn't care about hours and minutes):

我写了一个方法,应该能够将两个 Periods 与最近的一天进行比较(我不关心小时和分钟):

private int comparePeriods(Period period1, Period period2)
{
    if (period1.getYears() != period2.getYears())
    {
        return Integer.compare(period1.getYears(), period2.getYears());
    }
    if (period1.getMonths() != period2.getMonths())
    {
        return Integer.compare(period1.getMonths(), period2.getMonths());
    }
    if (period1.getWeeks() != period2.getWeeks())
    {
        return Integer.compare(period1.getWeeks(), period2.getWeeks());
    }
    if (period1.getDays() != period2.getDays())
    {
        return Integer.compare(period1.getDays(), period2.getDays());
    }
    return 0;
}

Note that this method expects both periods to be normalised or it will not give accurate results.

请注意,此方法期望两个时期都被标准化,否则不会给出准确的结果。