0

我正在尝试使用动态查找器搜索两个字段:statusOpenOn(日期)。

render(view:'list', model:[incidentInstanceList:Incident.findAllByStatusIlikeAndOpenOnGreaterThan("closed",new Date()-7,[sort:"id",order:"desc"])]) 

上面的查询搜索过去 7 天,但我想搜索“上周”,而不是过去 7 天。我怎样才能做到这一点?

4

1 回答 1

1

你可能想要这样的东西:

def lastWeek
use(org.codehaus.groovy.runtime.TimeCategory) {
    lastWeek = new Date() - 1.week
}

render(view:'list', model: [incidentInstanceList: Incident.findAllByStatusIlikeAndOpenOnGreaterThan( "closed", lastWeek, [sort:"id", order: "desc"])] )

更新:

import java.util.Calendar
import groovy.time.TimeCategory

def roundToLastMonday(date) {
    Calendar cal=Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - cal.get(Calendar.DAY_OF_WEEK) + Calendar.MONDAY)
    cal.getTime()
}

def getLastWeekRange() {
    def startDate, endDate
    use(TimeCategory) {
      startDate = roundToLastMonday(1.week.ago)
      endDate = startDate + 1.week - 1.second
    }
    [startDate, endDate]
}

def range = getLastWeekRange()
def result = Incident.withCriteria {
    like ("status", "closed")
    between ("open", range[0], range[1])
}
render(view:'list', model: [incidentInstanceList: result]
于 2011-08-26T08:47:36.073 回答