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
參考: