简体   繁体   English

如何在freemarker中将日期转换为字符串?

[英]How to convert date to string in freemarker?

i did this way.. 我是这样的。

<#assign createdOn='${receipt.arPaymDate}'>
${createdOn?datetime("MMM dd yyyy HH:mm:ss 'GMT'Z")?date}

But i am getting below execption 但是我变得无法执行

Can't convert the date-like value to string because it isn't known if it's a date (no time part), time or date-time value. 无法将类似日期的值转换为字符串,因为不知道它是日期(无时间部分),时间还是日期时间值。 The blamed expression: ==> receipt.arPaymDate [in template "WEB-INF/ftl/receiptPdf.ftl" at line 22, column 47] 责备的表达式:==> receive.arPaymDate [在模板“ WEB-INF / ftl / receiptPdf.ftl”中的第22行,第47列]

thanks in advened 谢谢

You may need first init the date format 您可能需要先初始化日期格式

<#setting date_format="dd-MM-yyyy">

<#assign createdOn='${receipt.arPaymDate}'> 
${createdOn?datetime("MMM dd yyyy HH:mm:ss 'GMT'Z")?date}

Also see How to convert date in specific format in Freemarker template or javascript 另请参阅如何在Freemarker模板或javascript中以特定格式转换日期

It depends on what's the type of receipt.arPaymDate is. 这取决于receipt.arPaymDate是什么类型。

In any case, you shouldn't write <#assign createdOn='${receipt.arPaymDate}'> , but <#assign createdOn=receipt.arPaymDate> , as the former converts receipt.arPaymDate to a string (as you insert into an empty string there), even if it was already a java.util.Date . 在任何情况下,你不应该写<#assign createdOn='${receipt.arPaymDate}'><#assign createdOn=receipt.arPaymDate>因为前者转换receipt.arPaymDate为一个字符串(如你插入一个空字符串),即使它已经是一个java.util.Date If it was a String , then it does nothing, so it's needless verbosity. 如果它是String ,那么它什么都不做,所以它是不必要的冗长。

If receipt.arPaymDate is a java.util.Date , and you do the assignment without the '${...}' , you don't need the ?datetime(...) part, only the ?date part: ${createdOn?date} . 如果receipt.arPaymDate是一个java.util.Date ,并且您在不使用'${...}'情况下进行赋值,则不需要?datetime(...)部分,只需要?date部分: ${createdOn?date} (Even the ?date can be omitted if receipt.arPaymDate is a java.sql.Date , as that's known to store the date part only, not time.) (如果receipt.arPaymDatejava.sql.Date ,则即使可以省略?date ,因为众所周知,该日期仅存储日期部分,而不存储时间。)

If receipt.arPaymDate is a string, so you must parse it to date, you can just write ${createdOn?date("MMM dd yyyy HH:mm:ss 'GMT'Z")} . 如果receipt.arPaymDate是字符串,那么您必须将其解析为最新日期,您只需编写${createdOn?date("MMM dd yyyy HH:mm:ss 'GMT'Z")} It's not a problem that the pattern has time part, ?date will still mark the result as a date-only value. 模式具有时间部分不是问题, ?date仍会将结果标记为仅日期的值。

Also, just in case it wasn't clear, the assignment in this example is unnecessary. 另外,以防万一,在此示例中的分配是不必要的。 One can just write ${receipt.arPaymDate?date} . 可以只写${receipt.arPaymDate?date}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM