比较两个日期。源码

时间:2016-04-28 11:14:54

标签: android database sqlite date datetime

我有一个sqlite数据库,其中包含一列任务,其中包含一列dueDates。我想过滤掉大于明天日期的日期。因此,如果明天的日期是4月29日,我应该比明天晚些时候得到日期。

为此,我已将字符串转换为日期。格式为:

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

,查询是:

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

但是我的列表为0.没有日期。也许我的查询错了。请帮忙。

编辑:正如所建议的那样。

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

    String selectQuery = "SELECT * FROM " + TASK_TABLE ;

    Date dueDate,tomorrowDate;
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat();

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    try {
    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)));


            dueDate = simpleDateFormat1.parse(task.getDueDate());
            tomorrowDate = simpleDateFormat1.parse(tomorrow);
            if (dueDate.getTime() > tomorrowDate.getTime()) {
                conList.add(task);
            }


            Log.d("laterQuery", selectQuery);

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

    catch (ParseException e) {

    }

    return conList;
}

仍然列表返回0.

同样我想知道有多少任务的日期晚于明天。

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


    String selectQuery = "SELECT * FROM " + TASK_TABLE + " WHERE " + KEY_TASK_STATUS + " = 0";
    Date dueDate,tomorrowDate;
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("d MMM yyyy", Locale.ENGLISH);

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

    if (mCount.moveToFirst()) {
        do {

            count = mCount.getInt(0);
        } while (mCount.moveToNext());
    }

    Log.d("query",selectQuery);

    Log.d("count",String.valueOf(count));
    mCount.close();

    return count;
}

如何添加?

3 个答案:

答案 0 :(得分:0)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String to_date = sdf.format(new Date());

crs = db.rawQuery(
      "SELECT _id, date_from, date_to, done_or_not, list_name " +
      "FROM list_tbl " +
      "WHERE " +
      "date_from <= Datetime('"+to_date+"') AND date_to >= Datetime('"+to_date+"')", null);

答案 1 :(得分:0)

只需将过滤器放入查询结果中即可。

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

String selectQuery = "SELECT * FROM " + TASK_TABLE ;

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

 try {
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)));


        dueDate = simpleDateFormat1.parse(task.getDueDate());
        tomorrowDate = simpleDateFormat1.parse(tomorrow);
        if (dueDate.getTime() > tomorrowDate.getTime()) {
            conList.add(task);
        }
    } while (cursor.moveToNext());
  }
}

catch (ParseException e) {

}

return conList;

}

答案 2 :(得分:0)

如果您想比较数据库中的任何两个日期或在数据库上应用查询,您的日期格式可以采用以下任何格式:

YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS

//您的查询

String selectQuery = "SELECT * FROM " + TASK_TABLE + " WHERE " + KEY_DUE_DATE + " > '" +2016-04-30 + "'" ;

或示例

SELECT * FROM test WHERE date BETWEEN "2011-01-11" AND "2011-8-11"

//下面的方法更改您的日期格式,如果您想在UI上以不同的格式显示日期

//
/**
 * Change date for mate to original to target date format
 * return formatted date string
 * @param myDate
 * @return String
 */
public static String changeDateFormatIntoYYYYMMDDHHMMSS(String myDate,
                                                        String originalDateFormat,
                                                        String targetDateFormat){
    String formattedDate="";
    try {
        DateFormat originalFormat = new SimpleDateFormat(originalDateFormat);//AppConstant.DATE_FORMAT_FOR_UI);
        DateFormat targetFormat = new SimpleDateFormat(targetDateFormat);//AppConstant.DATE_FORMAT_FOR_DB);
        Date date = originalFormat.parse(myDate);
        formattedDate = targetFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return formattedDate;
}

// calling Example 
String changeDate= changeDateFormatIntoYYYYMMDDHHMMSS("2016/04/30", DATE_FORMAT_FOR_DB,DATE_FORMAT_FOR_UI);

// String Formate
String DATE_FORMAT_FOR_DB="yyyy/MM/dd HH:mm:ss";
String DATE_FORMAT_FOR_UI="MMM dd, yyyy hh:mm a";

本“准则”有助于您实现自己的功能