8

How do I convert a datetime field in Grails to just date, with out capturing the time? I need to do this for comparison with system date.

class Trip 
{
    String name
    String city
    Date startDate
    Date endDate
    String purpose
    String notes

    static constraints = {
        name(maxLength: 50, blank: false)
        startDate(validator: {return (it >= new Date())}) // This won't work as it compares the time as well 
        city(maxLength: 30, blank: false)
    }
}
4

8 回答 8

7

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()` 
于 2008-12-08T13:50:02.603 回答
3

I cracked it :

startDate(validator: {return (it >= new Date()-1)})

It was that simple ;-)

To change the view in GSP page:

<g:datePicker name="startDate" value="${trip?.startDate}" years="${years}"  precision="day" />

Thanks everyone for the contribution

于 2008-12-09T07:51:31.657 回答
3

Better use calender plugin in Grails.

于 2009-02-25T10:42:51.670 回答
2

You should use startdate.clearTime()

We do this by overwriting the setter for our domain classes that only need the date and not the time. That way, we can compare the dates of two instances without having to do this later. :

def setStartDate( Date date ) {
    date.clearTime()
    startDate = date
}
于 2012-10-15T16:49:40.457 回答
1

Try using 'java.sql.Date' not 'java.util.Date' as a type of your Date property along with

formatDate

Purpose

Allows the formatting of java.util.Date instances using the same patterns defined by the SimpleDateFormat class.

Examples

Description

Attributes

* format (required) - The format to use for the date
* date (required) - The date object to format
于 2008-12-08T12:49:41.990 回答
1

Late I know, but these days, don't use Date, use LocalDate. Works fine in Grails / groovy GORM, and it's the new Java way of doing things.

于 2021-03-21T10:55:34.870 回答
0

Maybe

startDate(validator: {d = new Date(); return (it..d) >= 0})
于 2008-12-08T12:56:10.153 回答
0

Have you tried using jodatime? It makes working with date and time in java so much easier.

于 2008-12-08T13:02:05.383 回答