Freemarker日期函数处理

来源:互联网 发布:淘宝不锈钢企鹅杯授权 编辑:程序博客网 时间:2024/05/21 22:28
如直接将date型的放入freemaker的前台,那么只会显示年月日,因为默认的实际是 ${attendance.enterDate?date}
下面给出一些参考的日期处理:

string(当和一个日期值一起使用)

 这个内置标签用指定的格式把日期转换成字符串,(把默认的格式用FreeMarker的ate_format,time_format和datetime_format设置指定对你有好处,那样的话你就不需要这个标签了。

格式可以是一个预定义的,你也可以明确指定格式。

 预定义的格式是:short,medium,long和full。定义了结果字符串的长度。例如,如果locale是US_EN,时区是US.PACIFIC,那么:
${openingTime?string.short}
${openingTime?string.medium}
${openingTime?string.long}
${openingTime?string.full}
${nextDiscountDay?string.short}
${nextDiscountDay?string.medium}
${nextDiscountDay?string.long}
${nextDiscountDay?string.full}
${lastUpdated?string.short}
${lastUpdated?string.medium}
${lastUpdated?string.long}
${lastUpdated?string.full}
输出类似这样:

12:45 PM
12:45:09 PM
12:45:09 PM CEST
12:45:09 PM CEST
4/20/07
Apr 20, 2007
April 20, 2007
Friday, April 20, 2007
4/20/07 12:45 PM
Apr 20, 2007 12:45:09 PM
April 20, 2007 12:45:09 PM CEST
Friday, April 20, 2007 12:45:09 PM CEST

short,medium.long和full准确的意思依赖于当前locale(语言),此外,这是你运行FreeMarker的java实现平台所指定的,而不是FreeMarker。
对于即包含日期和时间的日期值,你可以单独的指定日期和时间部分的长度。

${lastUpdated?string.short_long} <#-- short date, long time -->
${lastUpdated?string.medium_short} <#-- medium date, short time -->

 将会输出:

4/8/03 9:24:44 PM PDT
Apr 8, 2003 9:24 PM

注意:string.short跟?string.short_short是一样的,?string.medium和string.medium_medium一样……
警告:

不幸的是,由于java平台的限制。当你在Data Model中存有日期值的时候,FreeMarker不能决定该变量只存储日期部分或者时间部分再或者日期和时间。这种情况下当你像${lastUpdated?string.short}或者简单的${lastUpdated}这样写的时候,FreeMarker不知道如何显示日期。这样它会停下来,并且报错。为了防止这样,你可以使用?date,?time和?datetime内置标签来帮助FreeMarker。举例:${lastUpdated?datetime?string.short}.询问程序员某个日期变量是否存在这个问题,或者一直使用?date,?time和?datetime。

你可以使用?string(格式)明确指定格式,代替预定义格式。格式使用java日期格式语法例如:

${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
${lastUpdated?string("EEE, MMM d, ''yy")}
${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}

 将会输出:
2003-04-08 21:24:44 Pacific Daylight Time
Tue, Apr 8, '03
 Tuesday, April 08, 2003, 09:24:44 PM (PDT)

 注意:

 不像预定义格式,你不需要在指定的格式上使用?date,?time和?datetime,因为你指定的格式告诉FreeMarKer显示日期的哪部分。无论如何,FreeMarker都会相信你,so you can show "noise" if you display parts that are actually not stored in the variable.例如:${openingTime?string("yyyy-mm-dd hh:mm:ss a")},openingTime只存储了时间。将会显示1790-01-01 09:24:44 PM.

 格式也可以是short,medium……"short_medium"等等。这样跟你用"."使用预定义的格式是一样的:someDate?string("short")和someDate?string.short是相当的。

date,time,datetime
这些标签可以用来指定日期变量中的哪些部分被使用。
date:只使用年、月、日
time:只使用时、分、秒和毫秒部分
datetime:日期和时间两部分都被使用
理想情况下,你不需要使用它们。不幸的是,由于java平台的技术限制。FreeMarker有的时候不能找到日期变量使用的部分(例如:只有年月日,或者只有时分秒,或者两者)询问程序员那个变量存在这个问题。如果FreeMarker需要执行一个需要这个变量的操作--就像把日期作为字符显示--但是它不知道使用那些部分,它会停下来报错。这就是你必须使用这些标签的情况。例如:假定openingTime就是这样一个问题变量:

<#assign x = openingTime> <#-- no problem can occur here -->
 ${openingTime?time} <#-- without ?time it would fail -->
 <#-- For the sake of better understanding, consider this: -->
 <#assign openingTime = openingTime?time>
 ${openingTime} <#-- this will work now -->
 另一种用法:切短日期。例如:
Last updated: ${lastUpdated} <#-- assume that lastUpdated is a date-time value -->
Last updated date: ${lastUpdated?date}
Last updated time: ${lastUpdated?time}

将显示:
Last updated: 04/25/2003 08:00:54 PM
Last updated date: 04/25/2003
Last updated time: 08:00:54 PM

0 0
原创粉丝点击