Android日期查询无法正常工作

时间:2014-11-10 06:41:12

标签: android database sqlite

我正在创建一个应用,我必须在gps位置发生变化的某个时间存储日期和时间。我在从SQLITE检索数据时遇到问题。我正在使用这两个函数来获取当前日期和时间并将它们存储在DataBase中。

public static String getCurentDate() {
    Calendar calender = Calendar.getInstance();
    return String.valueOf(calender.get(Calendar.MONTH) + 1) + "/"
            + String.valueOf(calender.get(Calendar.DAY_OF_MONTH)) + "/"
            + String.valueOf(calender.get(Calendar.YEAR));
}

public static String getCurentTime() {
    Calendar calender = Calendar.getInstance();
    String strHour = String.valueOf(calender.get(Calendar.HOUR_OF_DAY));
    String strMinute = String.valueOf(calender.get(Calendar.MINUTE));
    if (strMinute.length() == 1)
        strMinute = "0" + strMinute;
    return strHour + ":" + strMinute;
}  

我正在使用这3个函数来获取从DB中检索的数据。

public ArrayList<LocationItem> getLocationItemsByDateTime(String timeFrom,
            String timeTo, String dateFrom, String dateTo) throws Exception {
        Cursor cur;
        ArrayList<LocationItem> locationItems = new ArrayList<LocationItem>();
        try {
            openDB(true);
            cur = dBase.rawQuery("select * from "
                    + LocationItemTable.TABLE_NAME + " where "
                    + LocationItemTable.COL_TIME + " between '" + timeFrom
                    + "' and '" + timeTo + "' and "
                    + LocationItemTable.COL_DATE + " between '" + dateFrom
                    + "' and '" + dateTo + "'", null);
            if (cur.moveToFirst()) {
                do {
                    locationItems
                            .add(new LocationItem(cur.getInt(0), new Gson()
                                    .fromJson(cur.getString(1), LatLng.class),
                                    cur.getString(2), cur.getString(3)));
                } while (cur.moveToNext());
            }
            cur.close();
            return locationItems;
        } catch (Exception ex) {
            throw ex;
        } finally {
            closeDB();
        }
    }

public ArrayList<LocationItem> getLocationItemsByTime(String timeFrom,
        String timeTo) throws Exception {
    Cursor cur;
    ArrayList<LocationItem> locationItems = new ArrayList<LocationItem>();
    try {
        openDB(true);
        cur = dBase.rawQuery("select * from "
                + LocationItemTable.TABLE_NAME + " where "
                + LocationItemTable.COL_TIME + " between '" + timeFrom
                + "' and '" + timeTo + "'", null);
        if (cur.moveToFirst()) {
            do {
                locationItems
                        .add(new LocationItem(cur.getInt(0), new Gson()
                                .fromJson(cur.getString(1), LatLng.class),
                                cur.getString(2), cur.getString(3)));
            } while (cur.moveToNext());
        }
        cur.close();
        return locationItems;
    } catch (Exception ex) {
        throw ex;
    } finally {
        closeDB();
    }
}

public ArrayList<LocationItem> getLocationItemsByDate(String dateFrom,
        String dateTo) throws Exception {
    Cursor cur;
    ArrayList<LocationItem> locationItems = new ArrayList<LocationItem>();
    try {
        openDB(true);
        cur = dBase.rawQuery("select * from "
                + LocationItemTable.TABLE_NAME + " where "
                + LocationItemTable.COL_DATE + " between '" + dateFrom
                + "' and '" + dateTo + "'", null);
        if (cur.moveToFirst()) {
            do {
                locationItems
                        .add(new LocationItem(cur.getInt(0), new Gson()
                                .fromJson(cur.getString(1), LatLng.class),
                                cur.getString(2), cur.getString(3)));
            } while (cur.moveToNext());
        }
        cur.close();
        return locationItems;
    } catch (Exception ex) {
        throw ex;
    } finally {
        closeDB();
    }
}

但是这些查询并不适用于所有可能性。就像我输入时间10:00到20:00它给出结果但是如果我输入11:00到20:00查询返回null。同样的情况是日期

1 个答案:

答案 0 :(得分:1)

将字符串作为字符串进行比较,因此2/1/2000大于11/30/2014

要获得正确的比较,请使用yyyy-mm-ddhh:mm:ss等格式。 (这些也恰好是built-in date functions支持的格式。)