2011-10-21 107 views
4

我有一个csv文件,我想用fmpp(freemarker)进行转换。第一列是一个很长的值(从1970年1月1日开始,毫秒),我想将其转换为日期并将其格式化为日期时间。Freemarker模型以毫秒为单位转换时间戳

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。

有什么想法?

回答

19

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

Mayble您将不得不升级FMPP中的FreeMarker。为此,只需将<FMPP_HOME>/lib/freemarker.jar替换为最新版本即可。

+2

感谢名单!我必须在文档中监督这一点。给别人一个提示。您必须首先将字符串转换为数字,然后再转换为像这样的日期时间:$ {(row [h])?number?number_to_datetime}。输出的日期时间格式可以设置为:<#setting datetime_format =“yyyy-MM-dd hh:mm:ss”> – Andreas

相关问题