Re: Formatting a date
Marc Palmer <[email protected]> Mon, 27 Mar 2006 11:28:42 +0100
| Newsgroups | gmane.comp.java.webmacro.user |
|---|---|
| Message-ID | <[email protected]> |
On 27 Mar 2006, at 11:13, Alex Twisleton-Wykeham-Fiennes wrote: > On Mon 27 March 2006 11:00, Marc Palmer wrote: >> On 27 Mar 2006, at 02:47, Sven Schliesing wrote: >>> That's exactly what I looked for. >> >> There's another, potentially cleaner way... >> >> ----------------- "mymacros.wmm": >> >> #macro formatDate($d) >> $Text.formatDate($d, "dd.MM.yyyy, HH:mm") >> #end >> >> ----------------- "yourtemplate.wmt": >> >> #include as macro "mymacros.wmm" >> >> #formatDate($createdAt) > > Just to jump in for a moment - this will create a new > SimpleDateFormat object > every single time that it is invoked. If you know that you are > going to be > formatting a lot of different dates with the same format then it is > considerably more efficient to drop in a dedicated wrapper around > an instance > of SimpleDateFormat that gives you the format that you want as > otherwise each > time that you invoke it, it will have to parse your "dd.MM.yyyy, > HH:mm" > format string, build the internal representation and then format > the Date > which is relatively expensive. You're absolutely right, but that can be dealt with using a macro plus a #bean to create a "static" shared instance of the formatter? Of course then this offers little benefit, if not making things worse, over using Keats' #bean approach... #formatDate($createdAt) vs. $sharedFormatter.format($createdAt) ...and with my own caveats about using "directive" style macros I think the latter, Keats' scenario is better. Ahhhh now I'm thinking in Spring terms, and changing the topic... hmm it would be really nice if we could add some kind of context provider that populates the context with all the beans from a Spring BeanContext :) Cheers ------------------------------------------------------- This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory! http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642