简体   繁体   中英

display date / save date in Grails

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)

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

Date.metaClass.toString = {
  -> format('dd/MM/yyyy')
}

另请参阅有关Grails日期解组的第一个答案

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM