2011-10-21 104 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

相關問題