Date conversion in Freemarker and freemarker date conversion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.