比较日期后获取记录

时间:2013-02-12 08:48:12

标签: android sqlite

  

我希望在与之比较之后检索所有记录   Parsed_Reminder_Date在当前日期的3天之前和之后,在   我的数据库Parsed_Reminder_Date是一个用于保存的列   date ant它的数据类型很长,这是我的代码

Calendar calendar = Calendar.getInstance(); // This will give you the current time.
        // Removing the timestamp from current time to point to todays date
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        calendar.add(Calendar.DATE, -3); // Will subtract 3 days from today.
        Date beforeThreeDays = calendar.getTime();

        calendar.add(Calendar.DATE, 6); // Will be your 3 days after today
        Date afterThreeDays = calendar.getTime();

        long threedaybefore=beforeThreeDays.getTime() ;
        long threedayafter=afterThreeDays.getTime() ;

        Log.i("beforeThreeDays",Long.toString(threedaybefore));
        Log.i("afterThreeDays",Long.toString(threedayafter));


        String Reminder_Main = "Reminder_Main",ID = "ID", Reminder_Date = "Reminder_Date", Parsed_Reminder_Date = "Parsed_Reminder_Date";
        Cursor remindercursor = sdb
                .rawQuery(
                        "select Reminder_Id,Reminder_Type,Reminder_Time,Reminder_Date,Reminder_Alert,Reminder_Done,Reminder_byodometer,Reminder_bydate from "
                                + Reminder_Main
                                + " where "                             
                                + ID 
                                + " = ? AND "
                                + Parsed_Reminder_Date
                                + " >= ? OR  " 
                                + Parsed_Reminder_Date 
                                + " <= ? ",
                        new String[] {summaryid, Long.toString(threedaybefore), Long.toString(threedayafter)});

        remindercursor.moveToFirst();

当我执行此操作时,我得到了所有我不想要的记录, 如果我错了,请纠正我

1 个答案:

答案 0 :(得分:0)

使用像这样的简单查询

String query = "select Reminder_Id,Reminder_Type,Reminder_Time,Reminder_Date,Reminder_Alert,Reminder_Done,Reminder_byodometer,Reminder_bydate from "
                            + Reminder_Main
                            + " where "                             
                            + ID 
                            + " = "+ summaryid +" AND "
                            + Parsed_Reminder_Date
                            + " >= "+ threedaybefore +" AND  " 
                            + Parsed_Reminder_Date 
                            + " <= " + threedayafter;
Cursor remindercursor = sdb.rawQuery(query, null);

或使用其他技巧

1)编写查询以获取所有数据;

String query = "select Reminder_Id,Reminder_Type,Reminder_Time,Reminder_Date,Reminder_Alert,Reminder_Done,Reminder_byodometer,Reminder_bydate from "
                            + Reminder_Main
                            + " where "                             
                            + ID 
                            + " = "+ summaryid;
Cursor remindercursor = sdb.rawQuery(query, null);
Date d = null
if (cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            do {
                d = (<use Date Parser Here as per your dateFormat>cursor.getString(<Index>));
                if(d.after(beforeThreeDays) && d.before(afterThreeDays)){
                        // go Ahead.....
                } 
            } while (cursor.moveToNext());
        }
    }
    cursor.close();