jeudi 28 avril 2016

Compare two dates. Sqlite

I have a sqlite database which has table of tasks which has a column of dueDates. I want to filter out the dates greater than tomorrow's date. So if tomorrow's date is 29 th april I should get the dates later than tomorrow.

For this I have converted string into date. The format is :

        Calendar calendar = Calendar.getInstance();

    dateToday  = calendar.getTime();
    SimpleDateFormat df = new SimpleDateFormat("d MMM yyyy");
     dueDateToday = df.format(dateToday);

    calendar.add(Calendar.DAY_OF_YEAR, 1);
     dueTomorrow = calendar.getTime();

     dueDateTomorrow = df.format(dueTomorrow);

and the query is:

 public ArrayList<Task> getDueDatesLater(String tomorrow) {
    ArrayList<Task> conList = new ArrayList<Task>();

    String selectQuery = "SELECT * FROM " + TASK_TABLE + " WHERE " + KEY_DUE_DATE + " > '" + tomorrow + "'" ;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {

            Task task = new Task();

            task.setId(Integer.parseInt(cursor.getString(0)));
            task.setTitle(cursor.getString(1));
            task.setTaskPriority(Integer.parseInt(cursor.getString(2)));
            task.setAlertDate(cursor.getString(3));
            task.setAlertTime(cursor.getString(4));
            task.setDueDate(cursor.getString(5));

            task.setDueTime(cursor.getString(6));
            task.setList(Integer.parseInt(cursor.getString(7)));
            task.setStatus(Integer.parseInt(cursor.getString(8)));

            task.setAlertId(Integer.parseInt(cursor.getString(9)));

            conList.add(task);
        } while (cursor.moveToNext());
    }

    return conList;
}

but I am getting list as 0. There is no dates. Maybe my query is wrong. Please help.

Aucun commentaire:

Enregistrer un commentaire