Freemarker在模板中打印日期

时间:2011-05-20 18:29:21

标签: templates datetime freemarker pojo pretty-print

我正在尝试在模板激活时打印当前日期。我已经读过我必须将新的Date()Java对象传递给模板,但我不知道如何做到这一点或将它放在代码中。

在这种情况下,有人知道如何将Java对象传递给模板吗?

谢谢!

3 个答案:

答案 0 :(得分:11)

实际上,您不必将new Date()传递给模板,因为将时间戳放入模板的输出中非常常见,因此FreeMarker会提供一个名为.now的{​​{3}}返回当前的日期和时间。您可以在模板中使用它,如下所示:

Page generated: ${.now}

(FreeMarker还包含用于格式化日期的不同内置插件:special variable

更新:仅适用于最新版本的FreeMarker,http://freemarker.org/docs/ref_builtins_date.html

答案 1 :(得分:3)

使用Freemarker的ObjectConstructor API创建日历对象和格式化程序对象,然后将两者结合起来打印日期:

<#-- 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}
  

实施的内置?new是一个安全漏洞。现在,它只允许您实例化一个实现freemarker.template.TemplateModel接口的java对象。如果您想要以前版本中存在的?new内置功能,请为您的模板提供freemarker.template.utility.ObjectConstructor类的实例。例如:

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

然后在模板中你可以这样做:

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

<强>参考

答案 2 :(得分:1)

${.now}是完美的答案。只是想添加一些其他方法来从日期

获取直接值
#-- 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"]} -->

将输出

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