There's [unfortunately] not an "out-of-the box" method for performing this operation in Grails|Groovy|Java
.
Somebody always throws in Joda-Time any time a java.util.Date
or java.util.Calendar
question is raised, but including yet another library is not always an option.
Most recently, for a similar problem, we created a DateTimeUtil
class with static
methods and something like the following to get a Date
only:
class DateTimeUtil {
// ...
public static Date getToday() {
return setMidnight(new Date())
}
public static Date getTomorrow() {
return (getToday() + 1) as Date
}
public static Date setMidnight(Date theDate) {
Calendar cal = Calendar.getInstance()
cal.setTime(theDate)
cal.set(Calendar.HOUR_OF_DAY, 0)
cal.set(Calendar.MINUTE, 0)
cal.set(Calendar.SECOND, 0)
cal.set(Calendar.MILLISECOND, 0)
cal.getTime()
}
//...
}
Then, in the validator, you can use
startDate(validator: {return (it.after(DateTimeUtil.today))}) //Groovy-ism - today implicitly invokes `getToday()`