Compare date only (without time) in JPA2 (JPQL)

atamanroman picture atamanroman · May 4, 2011 · Viewed 45.4k times · Source

Im trying to compare Calendars with JPA2. The query looks somewhat like that:

TypedQuery<X> q = em.createQuery("select r from Record r where r.calendar= :calendar", X.class);
Calendar c = foo(); // setting fields and stuff
q.setParameter("calendar", c);

This, however, compares the date + time. I want to know if MM:DD:YYYY is equal and do not care about the time. Is there a nice way to do that in JPA2 or do I have to create a native query?

I tried setting HH:MM:SS:... to zero before saving it in the db but I don't know if this is very wise, regarding time zones and daylight saving and stuff.

Answer

Ot&#225;vio Garcia picture Otávio Garcia · May 8, 2011
q.setParameter("calendar", c, TemporalType.DATE)

You can pass the TemporalType.DATE to setParameter method to truncate the date+time.