Andreas Andreas - 25 days ago 15
Java Question

Freemarker model convert timestamp in milliseconds to date

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>$<#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?

Answer

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_numToDate

You 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.

Comments