在写ftl模板代码时,有一个常见的功能,即后台返回一个列表,前端分页展示。如果列表项中有Date对象,直接展示会报错,这个时候需要对这个Date对象进行格式化处理。格式化处理有几种方法
- ?date
日期,没有具体到时间 - ?time
只有时间 - ?datetime
日期+时间 - ?string(regx)
按regx正则表达式格式化输出
举例:object对象有一个createTime属性,是一个date对象,需要在表格中展示出来。
<td>
<#if (object.createTime)??>
${(object.createTime?string('yyyy-MM-dd HH:mm:ss'))}
</#if>
</td>