Freemarker print date in template

user763222 picture user763222 · May 20, 2011 · Viewed 15.1k times · Source

I am trying to print the current date when the template is activated. I have read that I have to pass a new Date() Java object to the template, but I don't know how to do that or where to put it in the code.

Does someone know how to pass a Java object to the template in this case?

Thank you !!

Answer

Chaquotay picture Chaquotay · May 22, 2011

Actually you don't have to pass a new Date() to your template, because placing a timestamp into a template's output is quite common and therefore FreeMarker provides a special variable called .now which returns the current date and time. You can use it in your template like this:

Page generated: ${.now}

(FreeMarker also contains different built-ins for formatting dates: http://freemarker.org/docs/ref_builtins_date.html)

Update: Works only with the latest version of FreeMarker, 2.3.17.