Grails - find where date ranges overlap
grails, grails-orm
Solution
Two Ways to embrace Grails/GORM in this case:
Lazy:-
def today = new Date()
def query = MyObject.where {
startDate <= (today - 10) && (endDate == null || endDate >= today + 10)
}
def listOfMyObjects = query.list()
Eager:-
def today = new Date()
def listOfMyObjects = MyObject.findAll {//or find{} if you need the first occurance
startDate <= (today - 10) && (endDate == null || endDate >= today + 10)
}
Problem
I have a Grails domain object with a startDate and endDate property. What's the best way to find all those objects where the range [startDate, endDate] overlaps with a specified date range? I know how to do this in SQL but wonder if there's any Grails/GORM magic to do it more succinctly. Also, the endDate is an optional property. The SQL / JPQL query would be something like ``` from MyObject obj where obj.startDate <= ?1 and (obj.endDate is null OR obj.endDate >= ?2) ```