Anthony
Anthony

Reputation: 35938

How to compare date using createCriteria without the time

I have the following record in database

2016-07-08 11:30:36

I am writing the following criteria to compare the dates but I'm not getting any results back because I think it might be comparing the time part as well.

    Date reminderDate = new Date()
    println reminderDate //This prints: Fri Jul 08 12:45:56 EDT 2016
    def result = criteria.list {
        eq('expireDate', reminderDate)
    }

Upvotes: 0

Views: 3386

Answers (3)

Dónal
Dónal

Reputation: 187529

This is how I would do it:

Date startOfToday = new Date().clearTime()
Date startOfTomorrow = startOfToday + 1

def result = criteria.list {
    between 'expireDate', startOfToday, startOfTomorrow
}

Upvotes: 1

Manali Khanna
Manali Khanna

Reputation: 101

You could use following:

Date reminderDate = new Date().clearTime() 
println reminderDate // This prints: Fri Jul 08 00:00:00 EDT 2016

def result = criteria.list { 
  ge('expireDate', reminderDate) 
} 

Upvotes: 4

jja
jja

Reputation: 100

Try an sqlRestriction like:

sqlRestriction 'date(expire_date) = ?', [reminderDate.format('YYYY-MM-dd')]

Note that you have to use the database column name (which I have guessed is expire_date), not the HQL or GORM property name (expireDate). You may need to modify the date format for your database.

Upvotes: 0

Related Questions