7

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

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

汽车对象看起来是这样的:

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

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

4

1 回答 1

8

这是您的问题:您犯了剪切和粘贴错误。您将明天设置为今天,然后无意中将昨天设置为明天。没有哪天既大于明天又小于今天,因此您没有返回任何行。

将您的代码更改为如下所示:

Calendar tommorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);  // change yesterday to tomorrow
于 2011-09-16T11:36:23.377 回答