vikas
vikas

Reputation: 1506

grails ORM , how to search Date without time

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 ?

Upvotes: 7

Views: 1992

Answers (2)

Christopher Will
Christopher Will

Reputation: 3064

Still it looks like there is no convenient way to realize this. You need a small detour by computing the start of the day and the end of the day and use the between operator.

EDIT I just saw now rcgeorge23 gave you the right example for doing this.

Upvotes: -1

rcgeorge23
rcgeorge23

Reputation: 3694

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).

Upvotes: 12

Related Questions