Freemarker 模型将时间戳(以毫秒为单位)转换为日期
我有一个 csv 文件,我想用 fmpp (freemarker)进行转换。第一列是一个长值(自 1970 年 1 月 1 日起的毫秒数),我想将其转换为日期并将其格式化为日期时间。
src 格式:
timeStamp,elapsed,label,responseCode,threadName,dataType,success,bytes,URL,Latency
1319115474244,40142,Login,200,Login 1-2,text,true,862184,http://localhost:8080/xxx,5378
理想的目标格式:
timeStamp;elapsed;label;responseCode;threadName;dataType;success;bytes;URL;Latency
20.12.2011 13:45;40142;Login;200;Login 1-2;text;true;862184;http://localhost:8080/xxx;5378
我的(运行)模板:
<#list csv.headers as h>${h}<#if h_has_next>;</#if></#list>
<#list csv as row>
<#list csv.headers as h><#if h_index == 0>Do the date magic<#else>${(row[h]!"N/A")?string}</#if>$<#if h_has_next>;</#if></#list>
</#list>
对于第 0 列,我想进行转换。我不想编写包含日期的新模型。我的问题是,这可以在模板中完成而不修改 freemarker 或 fmpp.
有什么想法吗?
I have a csv-file which i want to transform with fmpp (freemarker). The first column is a long value (milliseconds since 1.1.1970) which i want to convert into a date and format it as datetime.
src format:
timeStamp,elapsed,label,responseCode,threadName,dataType,success,bytes,URL,Latency
1319115474244,40142,Login,200,Login 1-2,text,true,862184,http://localhost:8080/xxx,5378
desirable target format:
timeStamp;elapsed;label;responseCode;threadName;dataType;success;bytes;URL;Latency
20.12.2011 13:45;40142;Login;200;Login 1-2;text;true;862184;http://localhost:8080/xxx;5378
My (running) template:
<#list csv.headers as h>${h}<#if h_has_next>;</#if></#list>
<#list csv as row>
<#list csv.headers as h><#if h_index == 0>Do the date magic<#else>${(row[h]!"N/A")?string}</#if>lt;#if h_has_next>;</#if></#list>
</#list>
For column 0 I want to do the conversion. I DON'T want to write a new model which contains a date. My question is, can this be done in the template without modifying freemarker or fmpp.
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FreeMarker 2.3.17 为此引入了
?number_to_date
、?number_to_time
和?number_to_datetime
。请参阅:http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_numToDate您将还想设置日期/时间格式和区域;请参阅http://fmpp.sourceforge.net/settings.html#sect17
也许你会必须在 FMPP 中升级 FreeMarker。为此,只需将
/lib/freemarker.jar
替换为最新版本即可。FreeMarker 2.3.17 has introduced
?number_to_date
,?number_to_time
and?number_to_datetime
for that. See: http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_numToDateYou will also want to set the date/time format and zone; see http://fmpp.sourceforge.net/settings.html#sect17
Mayble you will have to upgrade FreeMarker in FMPP. For that, simply replace the
<FMPP_HOME>/lib/freemarker.jar
with the latest version.在我的情况下,我使用这个:
并将
number_to_date
替换为number_to_datetime
或number_to_time
;您可以根据需要将
yyyy.MM.dd
替换为YYYY-MM-dd HH:mm:ss
。检查这个:
http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_numToDate
In my situation, I use this:
And replace
number_to_date
withnumber_to_datetime
ornumber_to_time
;And you can replace
yyyy.MM.dd
withYYYY-MM-dd HH:mm:ss
as you need.check this:
http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_numToDate