標籤:
Freemarker日期函數處理【轉】 (2012-08-01 14:32:13)
轉載▼
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
Freemarker日期函數處理【轉】