java Freemarker 模型将时间戳(以毫秒为单位)转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7846105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Freemarker model convert timestamp in milliseconds to date
提问by Andreas
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.
我有一个 csv 文件,我想用 fmpp(freemarker)转换它。第一列是一个长值(自 1970 年 1 月以来的毫秒数),我想将其转换为日期并将其格式化为日期时间。
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.
对于第 0 列,我想进行转换。我不想写一个包含日期的新模型。我的问题是,是否可以在不修改 freemarker 或 fmpp 的情况下在模板中完成此操作。
any ideas?
有任何想法吗?
回答by ddekany
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
FreeMarker的2.3.17已经推出?number_to_date
,?number_to_time
并?number_to_datetime
为。请参阅: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
您还需要设置日期/时间格式和区域;见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.
也许您必须在 FMPP 中升级 FreeMarker。为此,只需将 替换<FMPP_HOME>/lib/freemarker.jar
为最新版本。
回答by Jerry Ni
In my situation, I use this:
在我的情况下,我使用这个:
${(timeStamp)?number_to_date?string("yyyy.MM.dd")}
${(timeStamp)?number_to_date?string("yyyy.MM.dd")}
And replace number_to_date
with number_to_datetime
or number_to_time
;
并替换number_to_date
为number_to_datetime
或number_to_time
;
And you can replace yyyy.MM.dd
with YYYY-MM-dd HH:mm:ss
as you need.
你可以替换yyyy.MM.dd
使用YYYY-MM-dd HH:mm:ss
,因为你需要。
check this: http://freemarker.org/docs/ref_builtins_expert.html#ref_builtin_numToDate
检查这个:http: //freemarker.org/docs/ref_builtins_expert.html#ref_builtin_numToDate