Java – I think I came up with the worst way to compare two dates; Is it possible to make it better?

I think I came up with the worst way to compare two dates; Is it possible to make it better?… here is a solution to the problem.

I think I came up with the worst way to compare two dates; Is it possible to make it better?

My application accepts user-supplied dates through a date picker fragment and stores them in a database with day, month, and year as separate columns of type string.

Now I’m creating a function that will later check each of these dates with the system’s current date. If the current date is earlier than the date entered by the user (and stored in the database), a flag variable is incremented.

The code is as follows:

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;
    }

As you can see, I have to compare all possible scenarios for future dates. And I personally don’t think it’s the most efficient way.
Any suggestions?

Solution

I’ve never really used dates, but I’ll outline an algorithm that can at least reduce the number of comparisons you need to make.

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
}

This also won’t be very efficient, and you can reduce the number of if statements by using && strategically (although compiler optimizations may do this for you).

A better approach is to save the date in UNIX time, then get the current UNIX time and compare the two longs. It doesn’t get any simpler than that.

You can also construct and use another Calendar object based on the stored date before() The original long-to-long comparison is more efficient than comparing two calendars.

Related Problems and Solutions