I have a table that has a column with type of dateTime(I’m using SQL Server). I want to query the table by the dateTime column. I want rows that have the same date or later in the dateTime column, but I want to give the date as a parameter. In short, dateTime must be greater or equal than a specified date.
The problem I have is that when I try to query with Date object using criteria API, it only gives rows from the next day. I’m suspecting that it tries to find find rows with the same millisecond or later. In JPA query language I can specify the Temporal type of the parameter(setParameter(“dateColumn”,myDate,Temporal.DATE)) but I can’t find anything similar in the CriteriaBuilder API. Any suggestions?
My code looks currently like this:
cb.greaterThanOrEqualTo(r.<Date>get(fieldName), beginDate));
Since I haven’t gotten an answer I’m going to assume there is now way to this other than programmatically in your application.
If I want to do a query for entities that were created after this weeks monday I have to create a Date object, set the date to monday and clear the hours, minutes, seconds and milliseconds from the Date object. Apache Commons Lang library has a helper class for just this situation called DateUtils. It has method truncate which can be used to get the unnecessary accuracy out of the Date object so you can use greatedThanOrEqual in CriteriaBuilder.