开发者

ORMLite query for date

开发者 https://www.devze.com 2023-04-05 03:18 出处:网络
I am trying to query for a date between low and hign value: Car car = new Car(1, \"octavia\"); car.setManufactured(Calendar.getInstance().getTime());

I am trying to query for a date between low and hign value:

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 object looks this way:

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

Unfortunaly I am getting no results. Where could be a problem?


Here's your problem: you made a cut & paste error. You set tomorrow equal to today, then inadvertently set yesterday equal to tomorrow. There are no days that are both greater than tomorrow and less than today, so you got no rows back.

Change your code to look like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消