On Mon, Sep 21, 2009 at 1:45 AM, Jean Couteau <couteau(a)codelutin.com> wrote:
/* Groovy Class : Date parser#* */
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class DateParser {
Date parse(toParse) {
def formatter = new SimpleDateFormat("dd/MM/yyyy");
return formatter.parse(toParse);
}
}
I did the same thing for a similar problem. You can also keep a copy of the
dateformatter on the groovy class, instead of recreating it each time... and
you might also want to consider setting the timezone to something
consistent, like GMT.
import org.apache.commons.io.FileUtils
import java.text.SimpleDateFormat
import java.util.TimeZone
import java.lang.Double
class My_Groovy {
def xwiki
def context
def SimpleDateFormat df
void setObjects(param_xwiki, param_context) {
this.xwiki = param_xwiki
this.context = param_context
this.df = new SimpleDateFormat('HH:mm:ss.SSS');
this.df.setTimeZone(TimeZone.getTimeZone("GMT"));
}
...
this.df.format( ... )
...
}
It's too bad there's no way of doing "new SimpleDateFormat" directly out
of
velocity (or any kind of object creation). Then there would be no need for
the extra complication, and memory footprint, and loading times, all for a
very trivial use of groovy. It'd be nice if there was a generic way to
dynamically load a class out of velocity, call "new" on it, etc. This might
even obviate the need for many of Xwiki's simpler Java plugins that just
provide access to an existing Java class as $class, and then let you call
static methods on that as $class.myStaticMethod().
Niels
http://nielsmayer.com