[英]I think I have come up with the worst way to compare two dates; is it possible to make it better?
我的應用程序通過datepicker片段接受用戶的日期,並將其存儲在數據庫中,其中日,月和年作為String類型的單個列。
現在,我正在創建一個函數,稍后將使用系統的當前日期檢查每個日期。 如果當前日期早於用戶輸入的日期(並存儲在數據庫中),則標志變量將遞增。
這是代碼:
public int checkDate() { //Method to check date and take action
//NOT COMPLETE. STILL FIGURING IT OUT.
String isstatus = "Ongoing";
String[] columns = new String[] {KEY_ROWID, DAY, MONTH, YEAR ,PROJECT_STATUS};
Cursor c = projectDatabase.query(DATABASE_TABLE, columns, PROJECT_STATUS + "=" + isstatus, null, null, null, null);
int result = 0;
int flag = 0;
int iRow = c.getColumnIndex(KEY_ROWID);
int iDay = c.getColumnIndex(DAY);
int iMonth = c.getColumnIndex(MONTH);
int iYear = c.getColumnIndex(YEAR);
final Calendar cal = Calendar.getInstance(); //fetch current system date
int cyear = cal.get(Calendar.YEAR);
int cmonth = cal.get(Calendar.MONTH);
int cday = cal.get(Calendar.DAY_OF_MONTH);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String id = c.getString(iRow);
int fday = Integer.parseInt(c.getString(iDay));
int fmonth = Integer.parseInt(c.getString(iMonth));
int fyear = Integer.parseInt(c.getString(iYear));
if(cday>fday && cmonth>fmonth && cyear>fyear) {
flag++;
updateStatus(id, "Missed");
}
else
if(cday>fday && cmonth==fmonth && cyear==fyear) {
flag++;
updateStatus(id, "Missed");
}
else
if(cday==fday && cmonth>fmonth && cyear>fyear) {
flag++;
updateStatus(id, "Missed");
}
else
if(cday==fday && cmonth==fmonth && cyear>fyear) {
flag++;
updateStatus(id, "Missed");
}
else
if(cmonth>fmonth && cyear>fyear) {
flag++;
updateStatus(id, "Missed");
}
else
if(cmonth>fmonth && cyear==fyear) {
flag++;
updateStatus(id, "Missed");
}
result = flag;
}
return result;
}
正如您所看到的,我必須比較未來日期的每種可能情況。 我個人認為這不是最有效的方法。 有什么建議?
我從來沒有真正使用過日期,但我會概述一種算法,至少可以減少你需要進行的比較次數。
if(currentYear > dataYear) {
//We're obviously past that date. Move on
} else if(currentYear == dataYear) {
//We're in the same year. Let's check for months
if(currentMonth > dataMonth) {
//Missed the date again, move on
} else if(currentMonth == dataMonth) {
//We're in the same year and the same month! Let's check days
if(currentDay > dataDay) {
//Date is still in the past. Keep moving on
} else if(currentDay == dataDay) {
//Date is today
} else {
//Date is in the future
}
}
} else {
//Date is in the past
}
這也不會是非常有效的,你可以通過使用&&
戰略性地減少if語句的數量(盡管編譯器優化可能會為你做到這一點)。
更好的方法是在UNIX時間保存日期,並獲取當前的UNIX時間並只比較兩個long。 不能比那簡單得多。
您還可以從存儲日期構造另一個Calendar對象並使用before() 。 但是,原始的長對比比較比兩個日歷更有效。
建議使用joda datetime庫進行日期比較,有一些有用的功能,如isBefore和isAfter
只需使用Date
進行比較
Date now = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(now); // make sure the time part is the same
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String id = c.getString(iRow);
int fday = Integer.parseInt(c.getString(iDay));
int fmonth = Integer.parseInt(c.getString(iMonth));
int fyear = Integer.parseInt(c.getString(iYear));
cal.set(Calendar.YEAR, fyear);
cal.set(Calendar.MONTH, fmonth);
cal.set(Calendar.DAY_OF_MONTH, fday);
Date d = cal.getTime();
if (now.after(d)) {
flag++;
updateStatus(id, "Missed");
}
result = flag;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.