views:

2919

answers:

6

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)
    }
}
A: 

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
Samiksha
A: 

maybe

startDate(validator: {d = new Date(); return (it..d) >= 0})

LenW
A: 

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

andHapp
+5  A: 

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()`
Ken Gentle
seems to be solution I was looking for, however for some weird reason it does not work. the validation is not working even after I added the new class. Is there something that I am missing?? Thanks
Omnipotent
Hmm... how is it "not working?" I mean, are there any errors, warnings or messages on the console? Does it just not invoke the validation? Is the validation returning "false positives?"
Ken Gentle
+3  A: 

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

Omnipotent
As long as its OK for "startDate" to be validated as later than or equal to 24 hours ago (ie, yesterday some time), Cool! I think, given my [limited] understanding of the problem, I'd drop the ">=" and go with ">" (or Date.after(Date))
Ken Gentle
+3  A: 

Better use calender plugin in Grails.

+1->It is very nice compare to datepicker tag.
BlackPanther