use*_*222 9 datetime templates freemarker pretty-print pojo
我试图在模板激活时打印当前日期.我已经读过,我必须将新的Date()Java对象传递给模板,但我不知道如何做到这一点或将其放在代码中.
在这种情况下,有人知道如何将Java对象传递给模板吗?
谢谢 !!
Cha*_*tay 12
其实你不必传递new Date()到您的模板,因为放置一个时间戳到模板的输出是相当普遍的,因此FreeMarker的提供了一个特殊变量称为.now返回当前日期和时间.您可以在模板中使用它,如下所示:
Page generated: ${.now}
Run Code Online (Sandbox Code Playgroud)
(FreeMarker还包含用于格式化日期的不同内置函数:http://freemarker.org/docs/ref_builtins_date.html)
更新:仅适用于最新版本的FreeMarker,2.3.17.
${.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"]} -->
Run Code Online (Sandbox Code Playgroud)
将输出
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
Run Code Online (Sandbox Code Playgroud)