0

Is there any way to save / view date in our prefered format ? like dd/MM/yyyy instead of using simple date format everytime I save the date fields ?

for example: I would like to have

class User {
 Date birthDay ; 
}

the user will save the birthDay as '31/12/1988' for example and I would like to display it as that ... without format("dd/MM/yyyy") everytime I found the date (or converting string to date) for saving / updating)

nightingale2k1
  • 10,095
  • 15
  • 70
  • 96

3 Answers3

3

You can use metaClass to display the date in wanted format.

Date.metaClass.toString = {
  -> format('dd/MM/yyyy')
}
amra
  • 16,125
  • 7
  • 50
  • 47
  • You set metaClass for a class. This means set it as soon as possible, e.g. in bootstrap.groovy. Another option is to set metaClass for needed instances, e.g. yourDate.metaClass.toString = {...} – amra Feb 11 '11 at 10:42
2

look also at the first answer on Grails Date unmarshalling

Community
  • 1
  • 1
Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
0

If you're talking about date display, you'd better register an own PropertyEditor - with it, will use PropertyEditor's formatting.

If it's about saving to database, you'll need to set up a [custom Hibernate type mapping][2].

[2]: http://grails.org/doc/1.0.x/guide/5. Object Relational Mapping (GORM).html#5.5.2.10%20Custom%20Hibernate%20Types

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91