我正在尝试使用动态查找器搜索两个字段:status
和OpenOn
(日期)。
render(view:'list', model:[incidentInstanceList:Incident.findAllByStatusIlikeAndOpenOnGreaterThan("closed",new Date()-7,[sort:"id",order:"desc"])])
上述查询搜索过去7天,但我想搜索“上周”,而不是过去7天。我怎么能这样做?
答案 0 :(得分: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]