[英]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.