grails - Overriding Groovy's java.util.Date toString method -
i have grails (version 2.5.0 ; groovy version 2.4.3) application had custompropertyeditorregistry override date formats when using fieldvalue.
i installed elasticserach grails plugin version 0.0.4.4 , after installation noticed custom property editor not working anymore. in attempt temporarily work around problem, decided override java.util.date's tostring() method using groovy's meta programming.
i added bootstrap.groovy:
date.metaclass.tostring = { return delegate.format("mm/dd/yyyy hh:mm") }
however, when went grails console (using grails console plugin):
new date("fri jun 12 12:36:02 edt 2015") string == "fri jun 12 12:36:02 edt 2015" new date("fri jun 12 12:36:02 edt 2015").tostring() == "06/12/2015 12:36" println(new date("fri jun 12 12:36:02 edt 2015")) // prints fri jun 12 12:36:02 edt 2015 println(new date("fri jun 12 12:36:02 edt 2015").tostring()) // prints 06/12/2015 12:36
any figuring out custom property issue and/or overriding date tostring() appreciated. have opened issue on elasticsearch grails plugin github, issue #115 well
edit:
i've performed more tests both grails , groovy.
i created new grails 2.5.0 application code in bootstrap.groovy:
date.metaclass.tostring = { return delegate.format("mm/dd/yyyy hh:mm") }
and added index.gsp view:
<ul> <li>new date().tostring() == ${new date().tostring()}</li> <li>new date() == ${new date()}</li> <li>new date() string == ${new date() string}</li> </ul> /*output: new date().tostring() == 06/15/2015 10:32 new date() == mon jun 15 10:32:33 edt 2015 new date() string == mon jun 15 10:32:33 edt 2015 */
i launched groovyconsole groovy version 2.4.3 code/output:
date.metaclass.tostring = { return delegate.format("mm/dd/yyyy hh:mm") } println(new date()) println(new date() string) println(new date().tostring()) /*output: 06/15/2015 10:38 mon jun 15 10:38:12 edt 2015 mon jun 15 10:38:12 edt 2015 */
so appear inconsistency in groovy not calling tostring() modified metaclass method, unless i'm doing incorrectly or misunderstanding something.
you said you're using groovy console, mean you're running grails console
, right? bootstrap doesn't run when launching console, in run-app
, tests. if run tostring
override in console , run sample code should work fine.
Comments
Post a Comment