Thursday, March 06, 2008

Grails Date Trick

If you have a Date field in one of your domain classes, for instance:
class Person{
String Name
Date Birthday
}
and you're struggling to figure out a way to get stuff into it without explicitly creating a date and converting strings or integers into it, Grails provides a handy (yet not documented?) set of properties for you. In our example, I could set birthday_year, birthday_month, birthday_day, birthday_hour, etc.. I found this by digging in the scaffolded views and figuring out how they render the tag. Now, maybe you don't have to dig.

4 comments:

Anonymous said...
This comment has been removed by a blog administrator.
Vyas said...

What are the properties?

Farrukh said...

Check yourself by changing following

class Person{
    String Name
    Date Birthday
}

with

class Person{
    String Name
    java.sql.Date Birthday
}

specifying the property as java.sql.Date will render date control without hours and minutes fields.

Sweta Desai said...

I am struggling with the problem with dates..

Is it like if I will be declaring my date property using java.util.date
then it will in day month and year format and not those string to int and int to string conversions thing?

Please somebody reply soon..
Thanks.