Filter a date property between a begin and end Dates with JDOQL
- by Sergio del Amo
I want to code a function to get a list of Entry objects whose date field is between a beginPeriod and endPeriod I post below a code snippet which works with a HACK. I have to substract a day from the begin period date. It seems the condition great or equal does not work.
Any idea why I have this issue?
public static List<Entry> getEntries(Date beginPeriod, Date endPeriod) {
/* TODO
* The great or equal condition does not seem to work in the filter below
* Substract a day and it seems to work
*/
Calendar calendar = Calendar.getInstance();
calendar.set(beginPeriod.getYear(), beginPeriod.getMonth(), beginPeriod.getDate() - 1);
beginPeriod = calendar.getTime();
PersistenceManager pm = JdoUtil.getPm();
Query q = pm.newQuery(Entry.class);
q.setFilter("this.date >= beginPeriodParam && this.date <= endPeriodParam");
q.declareParameters("java.util.Date beginPeriodParam, java.util.Date endPeriodParam");
List<Entry> entries = (List<Entry>) q.execute(beginPeriod,endPeriod);
return entries;
}