ORMLite查询日期

时间:2011-09-16 11:34:43

标签: android sqlite ormlite

我正在尝试查询低值和高值之间的日期:

Car car = new Car(1, "octavia");
car.setManufactured(Calendar.getInstance().getTime());
Dao<Car, Integer> carDao = getHelper().getCarDao();

carDao.create(car);

QueryBuilder<Car, Integer> carQb = carDao.queryBuilder();

Calendar yesteday = Calendar.getInstance();
yesteday.add(Calendar.DATE, -1);

Calendar tommorrow = Calendar.getInstance();
yesteday.add(Calendar.DATE, 1);

carQb.where().between("manufactured", yesteday.getTime(), tommorrow.getTime());
PreparedQuery<Car> query = carQb.prepare();

List<Car> cars = carDao.query(query);

Log.d(TAG, cars.get(0).toString());

Car对象看起来像这样:

public class Car {

    @DatabaseField(id = true)
    private int id;
    @DatabaseField
    String name;
    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    User user;
    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    private Brand brand;
    @DatabaseField(columnName="manufactured")
    private Date manufactured;
}

不幸的是我没有得到任何结果。哪里可能有问题?

1 个答案:

答案 0 :(得分:8)

这是你的问题:你做了一个切割&amp;粘贴错误。你明天等于今天,然后无意中将昨天等于明天。没有比明天更大的日子而且比今天还要少,所以你没有回来的行。

将您的代码更改为:

Calendar tommorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);  // change yesterday to tomorrow