I have to compare two dates in hibernate hql query. I am using java.util.Date in my java bean and using timestamp as datatype in mysql database.
select t from Task t where t.modifiedDate > t.endDate;
Above query compares time with date. What should i do to compare date in above query without time.
If you want to compare just the date part without considering time, you need to use DateFormat class to format the date into some format and then compare their String value. Alternatively, you can use joda-time which provides a class LocalDate, which represents a Date without time, similar to Java 8's LocalDate class.
In Oracle You Can Use HQL trunc() Date Function Another function you can use for HQL date comparison queries is the HQL trunc() function.
CompareTo(DateOnly) Compares the value of this instance to a specified DateOnly value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateOnly value.
See the Hibernate documentations for available date functions
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html
In section 14.10 notice this line
second(...), minute(...), hour(...), day(...), month(...), and year(...)
So this should work
... where year(t.endDate) > year(t.startDate)
and month(t.endDate) > month(t.startDate)
and day(t.endDate) > day(t.startDate)
The solution reported as satisfying the question is
... where DATE(t.endDate) > (t.startDate)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With