1

本のタイトル、追加日、本を返却する必要がある日付を保存するアプリを作成しています。以下に、where今日の日付から 2 日後までに返却予定のすべての本をリストビューとして返すステートメントを示します。ただし、機能しません。どこが間違っているのかわかりません。

    public Cursor fetchAllDue() {

    //get todays date
    Calendar cal=Calendar.getInstance();
    int currentDay=cal.get(Calendar.DAY_OF_MONTH);

    //Add days on to todays date
    cal.set(Calendar.DAY_OF_MONTH, currentDay+2);
    cal.getTime(); //Result

    //format back to string
    String formattedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(cal.getTime());


  //get todays date
    Calendar cal1=Calendar.getInstance();
    int currentDay1=cal1.get(Calendar.DAY_OF_MONTH);

    //Atodays date
    cal1.set(Calendar.DAY_OF_MONTH, currentDay1);
    cal1.getTime(); //Result

    //format back to string
    String formattedDate1 = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(cal1.getTime());


    //body = date added, needs to be changed
    return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
            KEY_BODY, KEY_EXTRA, KEY_DUE }, KEY_DUE + " BETWEEN 'formattedDate1' and 'formattedDate2'", null, null, null, null);
}

これは、「期日」に日付を入力するときのコードです。

                //convert string to int.
            int myNum = Integer.parseInt(editbox3.getText().toString());

            //get todays date
            Calendar cal=Calendar.getInstance();
            int currentDay=cal.get(Calendar.DAY_OF_MONTH);

            //Add days on to todays date
            cal.set(Calendar.DAY_OF_MONTH, currentDay+myNum);
            cal.getTime(); //Result

            //formate back to string
            String formattedDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(cal.getTime());


            bundle.putString(NotesDbAdapter.KEY_DUE,formattedDate
                    .toString());

日付はすべて文字列です。誰でも助けることができますか?

4

3 に答える 3

1

WebnetMobileの答えが正しいと思います。

INTEGERAndroid の SQLite では、日付の UNIX タイム スタンプを含むフィールドを常に使用します。そのため、クエリをより迅速に実行し、WHERE文字列から日付への変換の問題がないか、問題なく実行できます。

Date date;
...
long timeStamp = date.getTime();

クエリのパラメーターとして日付を使用することもできます。

Date from, to;
long timeStampFrom = from.getTime();
long timeStampTo = to.getTime();

String[] params = new String[] { String.valueOf(from), String.valueOf(to) };
c = db.rawQuery("SELECT * FROM " + getTableName() + " WHERE DateTime BETWEEN ? AND ?", params);

または、UNIX タイムスタンプを使用する場合:

String[] params = new String[] { String.valueOf(timeStampFrom ), String.valueOf(timeStampTo )) };
c = db.rawQuery("SELECT * FROM " + getTableName() + " WHERE DateTime BETWEEN ? AND ?", params);
于 2013-04-08T21:16:43.510 に答える