3

私のアプリは、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;
    }

ご覧のとおり、日付が先になる可能性のあるすべてのケースを比較する必要があります。個人的には、それは最も効率的な方法ではないと思います。何かアドバイス?

4

3 に答える 3

3

私は実際に日付を扱ったことがありませんが、少なくとも必要な比較の数を減らすアルゴリズムの概要を説明します。

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時間を取得して、2つのlongを比較することです。それよりもはるかに単純になることはできません。

保存した日付から別のCalendarオブジェクトを作成し、before()を使用することもできます。ただし、基本的な長い比較と長い比較は、2つのカレンダーを比較するよりも効率的です。

于 2013-03-15T19:50:40.007 に答える
2

日付の比較に joda datetime ライブラリを使用することをお勧めします。isBefore や isAfter などの便利な機能がいくつかあります

http://joda-time.sourceforge.net/

于 2013-03-15T20:00:44.840 に答える
1

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;
}
于 2013-03-15T20:06:11.540 に答える