12

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 !!

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
user763222
  • 121
  • 1
  • 1
  • 4

3 Answers3

15

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.

Chaquotay
  • 2,212
  • 1
  • 17
  • 22
  • Thank you for ur answer but I am getting this error: ParseException: Unknown built-in variable: now – user763222 May 23 '11 at 13:55
  • The way I am doing it is with ${content.metaData.modificationDate?string.short}. The content.metaData.modificationDate gives me the last modification of that template which is pretty much what I need. – user763222 May 23 '11 at 15:32
  • 4
    The special variable `.now` was introduced in version 2.3.17 (see http://freemarker.org/docs/versions_2_3_17.html), which was released about a week ago. If you cannot update you'll have to pass a the current date into the data model, e.g. as part of your root hashmap (see http://freemarker.org/docs/pgui_quickstart_createdatamodel.html and http://freemarker.org/docs/pgui_quickstart_merge.html for the details, if you don't already know those things). – Chaquotay May 23 '11 at 19:20
6

${.now} is the perfect answer. Just wanted to add few other ways to get direct values from date

#-- Predefined format names: -->

${openingTime?string.short}
${openingTime?string.medium}
${openingTime?string.long}
${openingTime?string.full}
${openingTime?string.xs} <#-- XSD xs:time -->
${openingTime?string.iso} <#-- ISO 8601 time -->

${.now?string.short}
${.now?string.medium}
${.now?string.long}
${.now?string.full}
${.now?string.xs} <#-- XSD xs:date -->
${.now?string.iso} <#-- ISO 8601 date -->

${.now?string.short}
${.now?string.medium}
${.now?string.long}
${.now?string.full}
${.now?string.medium_short} <#-- medium date, short time -->
${.now?string.xs} <#-- XSD xs:dateTime -->
${.now?string.iso} <#-- ISO 8601 combined date and time -->

<#-- Programmer-defined named format (@ + name): -->
${.now?string.@fileDate}

<#-- Advanced ISO 8601 and XSD formatting: -->
${.now?string.iso_m_u}
${.now?string.xs_ms_nz}

<#-- SimpleDateFormat patterns: -->
${.now?string["dd.MM.yyyy, HH:mm"]}
${.now?string["EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'"]}
${.now?string["EEE, MMM d, ''yy"]}
${.now?string.yyyy} <#-- Same as ${.now?string["yyyy"]} -->

will output

01:45 PM
01:45:09 PM
01:45:09 PM PST
01:45:09 PM PST
13:45:09-08:00
13:45:09-08:00

2/20/07
Apr 20, 2007
April 20, 2007
Friday, April 20, 2007
2007-02-20-08:00
2007-02-20

2/20/07 01:45 PM
Feb 20, 2007 01:45:09 PM
February 20, 2007 01:45:09 PM PST
Friday, February 20, 2007 01:45:09 PM PST
Feb 8, 2003 9:24 PM
2007-02-20T13:45:09-08:00
2007-02-20T13:45:09-08:00

Apr/20/2007 13:45

2007-02-20T21:45Z
2007-02-20T13:45:09.000

08.04.2003 21:24
Tuesday, April 08, 2003, 09:24 PM (PDT)
Tue, Apr 8, '03
2003

Source

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
3

Use the ObjectConstructor API of Freemarker to create a calendar object and a formatter object, then combine the two to print the date:

<#-- Create constructor object -->
<#assign objectConstructor = "freemarker.template.utility.ObjectConstructor"?new()>

<#-- Call calendar constructor -->
<#assign clock = objectConstructor("java.util.GregorianCalendar")>

<#-- Call formatter constructor -->
<#assign mmddyy = objectConstructor("java.text.SimpleDateFormat","MM/dd/yyyy")>

<#-- Call getTime method to return the date in milliseconds-->
<#assign date = clock.getTime()>

<#-- Call format method to pretty print the date -->
<#assign now = mmddyy.format(date)>

<#-- Display date -->
${now}

The ?new built-in, as it was implemented, was a security hole. Now, it only allows you to instantiate a java object that implements the freemarker.template.TemplateModel interface. If you want the functionality of the ?new built-in as it existed in prior versions, make available an instance of the freemarker.template.utility.ObjectConstructor class to your template. For example:

myDataModel.put("objConstructor", new ObjectConstructor());

and then in the template you can do this:

<#assign aList = objConstructor("java.util.ArrayList", 100)>)

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265