当前位置: 移动技术网 > IT编程>网页制作>Html5 > freemarker时间转换

freemarker时间转换

2018年12月13日  | 移动技术网IT编程  | 我要评论

酷乐猫全集,网上购票几点开始放票,我的阿姨

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网