[英]How can I compare two Calendar dates?
我有一個Java問題,需要檢查某項是否已過期。 這應該檢查項目是否至少存在x(x是整數,可以設置為任何整數值)個月。
只是為了澄清一下,假設我有一包雞蛋,我想檢查一下自添加dateAdded
( dateAdded
)到現在已經有1個月了。
我寫了一個簡單的比較,但似乎沒有給出正確的答案。 這是代碼。
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庫。
僅供參考:
minusMonths
方法很聰明,可以處理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
正如Calendar docs中提到的那樣,您不應該依賴compareTo返回的數字-您只知道如果原始數字大於0,則原始日期會更大。
因此,創建一個新日期(經過的x個月)並與該日期進行比較。
如果參數表示的時間等於此Calendar對象表示的時間,則該方法返回0;否則,方法返回0。 或小於0的值(如果此Calendar的時間早於參數表示的時間); 或大於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。感謝您的所有建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.