Date conversion in Freemarker and freemarker date conversion
1. Convert the date represented by the number type to the datetime type and output the string.
$ {Item. time? Number_to_datetime}. The default format is "yyyy-MM-dd hh: mm: ss ".
You can manually set the datetime format before calling, for example, <# setting datetime_format = "yyyy-MM-dd"/>
2. Convert the date represented by the date type to the string format and output the string.
$ {Item. time? String ("MM dd, yyyy ")}
3. If item. time is null, Freemarker reports an error, which is annoying.
So I wrote a common date tool function:
<# Function number_to_ymdhms date>
<# If date! = Null>
<# Setting datetime_format = "yyyy-MM-dd hh: mm: ss"/>
<# Return date? Number_to_datetime/>
</# If>
<# Return ""/>
</# Function>
<# Function date_to_ymdhms date>
<# If date! = Null>
<# Return date? String ('yyyy-MM-dd hh: mm: ss')/>
</# If>
<# Return ""/>
</# Function>
Introduction Method: <# include "/user/common/function/date. ftl"/>
Call method: $ {number_to_ymdhms (item. addtime )}
The advantage is that the date processing is unified to one place and the format is unified. If you do not need a uniform function, you need to write another function with a format.
Null must be determined to prevent the following error: null exception.
First: http://fansunion.cn/article/detail/531.html