[freemarker]Freemarker日期函数处理

 

string(当和一个日期值一块儿使用)html

 

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

 

格式能够是一个预约义的,你也能够明确指定格式。程序员

 

 预约义的格式是:short,medium,long和full。定义告终果字符串的长度。例如,若是locale是US_EN,时区是US.PACIFIC,那么:ide

 

${openingTime?string.short}this

 ${openingTime?string.medium}spa

 ${openingTime?string.long}orm

 ${openingTime?string.full}xml

 

${nextDiscountDay?string.short}htm

 ${nextDiscountDay?string.medium}blog

 ${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

转自:

http://blog.sina.com.cn/s/blog_a0e7e34c01017vku.html

参考:

http://jxdwuao.iteye.com/blog/738289

相关文章
相关标签/搜索