Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails ORM , how to search Date without time

Tags:

grails

I am using postrges db. My domain has a date field:

java.util.Date requestedDate;

I am trying to search by date in my controller:

eq ("requestedDate", requestedDate)

This works fine, but the problem is that date and time has to be exactly matching for this. But in application the user will only enter the date to search items (like give me all requests which are made on 2014-02-05 and the browser application will add the current time to the request). So the comparison fails because the user entered time is different from the time during creation of the request. I tried 'like' but it throws error.

How to compare only date part ?

like image 350
vikas Avatar asked Feb 05 '14 13:02

vikas


1 Answers

You could do something like this:

Date now = new Date()
now.clearTime()

def results = Meeting.withCriteria {
    between('date', now, now+1)
}

So this strips off the time portion of the current date, and then does a 'between' query (between midnight just gone and midnight 24 hours later).

like image 152
rcgeorge23 Avatar answered Oct 23 '22 17:10

rcgeorge23