Prathap
Prathap

Reputation: 727

Compare Date and time in Hibernate

I have a defined column notification_date as DATETIME in Database.

When the user filter I get only Date. So I'm converting as data and time using Java. For an Example if the userSearch Date is '2012-09-26'. The final converted date and time format will be '2012-09-26 17:34:00'

When I want to search Date filter operations like equal,Less than , Greater than and date between, I'm not able to get the proper result using Hibernate criteria Query for Greater than and Less than operation.

The code used

crit.add(Restrictions.gt("notificationDate",notificationVO.getNotificationDate()));

The restriction will change according to the condition

To compare only date, is there any functions available in Hibernate?

Any other idea to fix this issue?

Upvotes: 3

Views: 3761

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

From your comment:

If the user enters 2012-09-26 and search criteria as less than, I need all the dates which is less than 2012-09-26

Then the restriction should be:

crit.add(Restrictions.lt("notificationDate", notificationVO.getNotificationDate()));

If the user enters the same date, and the search criterion "greater than", and you use the following restriction:

crit.add(Restrictions.gt("notificationDate", notificationVO.getNotificationDate()));

You will of course get all the entries with a date and time greater than 2012-09-26 00:00:00, including of course all the entries of the 2012-09-26 date.

Upvotes: 1

Related Questions