Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORMLite query for date

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?

like image 720
sealskej Avatar asked Sep 16 '11 11:09

sealskej


1 Answers

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
like image 168
duffymo Avatar answered Nov 14 '22 22:11

duffymo