Search code examples
grailsgrails-ormcriteria

Grails query based on Date


imagine I have this domain class:

class Event {

    DateTime startDate
    DateTime endDate
}

I want to find Events that ended in the last 15 to 30 minutes. I would like to have something like:

def now = DateTime.now()

def events= Event.withCriteria {
    ge("endDate".plusMinutes(15), now)
    ge("endDate".plusMinutes(30), now)
}

Solution

  • you can do it also with plain Date magic

    def events= Event.withCriteria {
      between "endDate", new Date( now.time - 30 * 60 * 1000 ), new Date( now.time - 15 * 60 * 1000 )
    }