如何比较两个日历日期



我有一个Java问题,我需要检查项目是否已过期。这应该检查该项目是否至少是 x(x 是一个整数,可以设置为任何整数值)几个月前。

只是为了重新澄清假设我有一包鸡蛋,我想检查一下自从我添加它们以来是否已经 1 个月了(dateAdded )。

我写了一个简单的比较,但它似乎没有给出正确的回应。 这是代码。

public Boolean isEndOfLine() {
    Calendar today = Calendar.getInstance();
    if(today.compareTo(dateAdded) >= END_OF_LINE) {
        return true;
    } else {
        return false;
    }
}

行尾的值是整数 12,即 12 个月。

我脑子里没有javadoc,而是按照以下几点:

dateAdded.add(Calendar.Month, END_OF_LINE).compareTo(today) > 0

下面是一些类似的示例代码,但使用的是 Joda-Time 2.3 库。

仅供参考:

  • Joda-Time DateTime 实例知道自己的时区。
  • minusMonths方法很聪明,可以处理夏令时和其他问题。您可能需要阅读其源代码,以验证其逻辑是否遵循您的业务规则,即"x 个月前"的含义。
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
// Better to specify a time zone explicitly rather than rely on default.
// Time Zone list… http://joda-time.sourceforge.net/timezones.html  (not quite up-to-date, read page for details)
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
int countMonths = 2;
DateTime now = new DateTime( timeZone );
// If you want to include the entire day, get first moment of the day by calling "withTimeAtStartOfDay".
DateTime someMonthsAgo = now.minusMonths( countMonths ).withTimeAtStartOfDay();
DateTime dateAdded = new DateTime( 2013, 5, 6, 7, 8, 9, timeZone  ); // Arbitrary values for example.
// If 'dateAdded' happened prior to our target date-time 'someMonthsAgo', the pack of eggs is expired.
Boolean isEndOfLine = dateAdded.isBefore( someMonthsAgo );

转储到控制台...

System.out.println( "now: " + now );
System.out.println( "someMonthsAgo: " + someMonthsAgo );
System.out.println( "dateAdded: " + dateAdded );
System.out.println( "isEndOfLine: " + isEndOfLine );

运行时...

now: 2014-01-08T21:36:11.179+01:00
someMonthsAgo: 2013-11-08T00:00:00.000+01:00
dateAdded: 2013-05-06T07:08:09.000+02:00
isEndOfLine: true

日历文档中所述您不应该依赖 compareTo 返回的数字 - 您只知道如果它大于 0,则原始日期更大。

因此,创建一个新日期(过去了 x 个月)并与该日期进行比较。

如果参数表示的时间等于此 Calendar 对象表示的时间,则该方法返回 0;如果此日历的时间早于参数表示的时间,则返回小于 0 的值

;如果此日历的时间晚于所表示的时间,则

返回大于 0 的值。
    import java.util.*;
    public class CalendarDemo {
       public static void main(String[] args) {
          // create two calendar at the different dates
          Calendar cal1 = new GregorianCalendar(2015, 8, 15);
          Calendar cal2 = new GregorianCalendar(2008, 1, 02);
          // compare the time values represented by two calendar objects.
          int i = cal1.compareTo(cal2);
          // return positive value if equals else return negative value
          System.out.println("The result is :"+i);
          // compare again but with the two calendars swapped
          int j = cal2.compareTo(cal);
          // return positive value if equals else return negative value
          System.out.println("The result is :" + j);
       }
    }

这是工作解决方案。用JUNIT测试以确认结果。

public Boolean isEndOfLine() {
    Calendar today = Calendar.getInstance();
    today.add(Calendar.MONTH, -END_OF_LINE);
    return today.compareTo(dateAdded) >= 0;
}

我使用 add 方法从今天减去END_OF_LINE。请注意第 3 行的减号。然后我比较了一下它是否大于 0。感谢您的所有建议。

最新更新