14

I need a jpql query to find all the LoadFileHistory which finishDate be greater than the currente date (since 00:00:00). For instance greater than 27/11/2012 00:00:00.

I already have this one "select o from LoadFileHistory o where o.finishDate = CURRENT_DATE" but gets me nothing.

Manuel
  • 347
  • 2
  • 6
  • 14
  • 1
    If you want those which are greater why don't you use `>` instead of `=`? – Alex Nov 27 '12 at 14:18
  • Alex the idea of put = CURRENT_DATE is that the comparison were with the date but not with the time. I mean, just 27/11/2012 not 27/11/2012 10:15:00 – Manuel Nov 27 '12 at 15:16

2 Answers2

27

You should get today's date to the query like detailed here (java.util.Date has the hour, minute, second too...)

The you should supply it to your query:

Query q = em.createQuery("select o from LoadFileHistory o where o.finishDate > :today ");
q.setParameter("today",todaysDateObject,TemporalType.DATE);
q.getResultList();
Community
  • 1
  • 1
ppeterka
  • 20,583
  • 6
  • 63
  • 78
  • 7
    `CURRENT_DATE` is evaluated to the current date. See: http://www.objectdb.com/java/jpa/query/jpql/date – Alex Nov 27 '12 at 14:21
3

In case you are looking for time level filtering with date. This worked for me.

Date now = DateUtils.addMinutes(new Date(), -15);
Query q = em.createQuery("Select u From Users u Where u.dateCreated > :today");
q.setParameter("today",now,TemporalType.TIMESTAMP);

DateUtils is of apache common.

buræquete
  • 14,226
  • 4
  • 44
  • 89
A_01
  • 1,021
  • 11
  • 27
  • 2
    ... or you could do it without adding an external dependency and use java.time ... `Instant.now().minus(Duration.ofMinutes(15))` – Jules Feb 16 '18 at 21:03