我的应用程序通过日期选择器片段接受来自用户的日期,并将其存储在数据库中,其中日、月和年作为字符串类型的单独列。
现在,我正在创建一个函数,稍后将使用系统的当前日期检查每个日期。如果当前日期早于用户输入的日期(并存储在数据库中),则增加一个标志变量。
这是代码:
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;
}
如您所见,我必须比较未来日期的所有可能情况。而且我个人认为这不是最有效的方法。有什么建议吗?