Freemarker Date function Processing "Go"

Source: Internet
Author: User

Freemarker Date function Processing "Go" (2012-08-01 14:32:13) reproduced
Tags: gossip

String (when used with a date value)

This built-in tag converts dates into strings in the specified format, which is good for you by specifying the default format with Freemarker's ate_format,time_format and datetime_format settings. In that case, you don't need the label.

The format can be a predefined one, and you can explicitly specify the format.

The predefined formats are: Short,Medium,long, and full . Defines the length of the resulting string. For example, if locale is us_en, the time zone is US. PACIFIC, then:

${openingtime?string.short}

${openingtime?string.medium}

${openingtime?string.long}

${openingtime?string.full}

${nextdiscountday?string.short}

${nextdiscountday?string.medium}

${nextdiscountday?string.long}

${nextdiscountday?string.full}

${lastupdated?string.short}

${lastupdated?string.medium}

${lastupdated?string.long}

${lastupdated?string.full}

The output looks like this:

12:45 PM

12:45:09 PM

12:45:09 PM CEST

12:45:09 PM CEST

4/20/07

APR 20, 2007

April 20, 2007

Friday, April 20, 2007

4/20/07 12:45 PM

APR, 12:45:09 PM

April, 12:45:09 PM CEST

Friday, April, 12:45:09 PM CEST

The exact meaning of Short,medium.long and full is dependent on the current locale (language), and in addition, it is specified by the Java implementation platform that you run Freemarker, not the freemarker.

For date values that contain dates and times, you can specify the length of the date and time portions individually.

${lastupdated?string.short_long} <#--Short date, long time--

${lastupdated?string.medium_short} <#--Medium Date, short time--

The output will be:

4/8/03 9:24:44 PM PDT

APR 8, 2003 9:24 PM

Note:string.short and String.short_short are the same,String.medium and string.medium_medium ...

Warning:

Unfortunately, due to the limitations of the Java platform. When you have a date value in the Data model,Freemarker cannot determine whether the variable stores only the date part or the time part, or the date and time. In this case, when you write like ${lastupdated?string.short} or a simple ${lastupdated},Freemarker does not know how to display the date. So it will stop and error. To prevent this, you can use the date,?time and ? DateTime built-in tags to help freemarker. Example : ${lastupdated?datetime?string.short}. Ask the programmer if a date variable exists for this problem, or always use the ? Date,?time and ? DateTime.

You can use the. String format to specify the format explicitly instead of the predefined format. The format uses the Java date format syntax for example:

${lastupdated?string ("Yyyy-mm-dd HH:mm:ss zzzz")}

${lastupdated?string ("EEE, MMM D, ' yy")}

${lastupdated?string ("eeee, MMMM dd, yyyy, HH:MM:SS a ' (' zzz ') ')}

The output will be:

2003-04-08 21:24:44 Pacific Daylight Time

Tue, APR 8, ' 03

Tuesday, April, 2003, 09:24:44 PM (PDT)

Attention:

Unlike the predefined format, you do not need to use the. Date,?time and datetime in the specified format, because the format you specify tells Freemarker which part of the date is displayed. Anyway,Freemarker will believe you, socan show "noise" if you display parts that is actually not stored in the variable. For example: ${openingtime?string ("Yyyy-mm-dd hh:mm:ss a")},openingtime only stored time. The 1790-01-01 09:24:44 PM will be displayed.

The format can also be short,medium ... "Short_medium" and so on. It's a "." For you. The use of the predefined format is the same : somedate?string ("short") and Somedate?string.short are equivalent.

Date,time,datetime

These tags can be used to specify which parts of the date variable are used.

Date: Use only year, month, day

Time: Use only the hours, minutes, seconds, and milliseconds sections

DateTime: Date and time both parts are used

Ideally, you don't need to use them. Unfortunately, due to the technical limitations of the Java platform. Freemarker Sometimes it is not possible to find the part of the date variable (for example, only a month or two, or both) and ask the programmer about the variable that has the problem. If Freemarker needs to perform an operation that requires this variable--like displaying a date as a character--but it doesn't know which part to use, it will stop to give an error. This is the case where you have to use these tags. For example, suppose openingtime is such a problem variable:

< #assign x = openingtime> <#--No problem can occur here---

${openingtime?time} <#--without? Time it would fail-

<#--for the sake of better understanding, consider this:-

< #assign Openingtime = openingtime?time>

${openingtime} <#--This would work now--

Another usage: Cut a short date. For example:

Last Updated: ${lastupdated} <#--assume this lastupdated is a date-time value-

Last Updated Date: ${lastupdated?date}

Last Updated time: ${lastupdated?time}

will display:

Last updated:04/25/2003 08:00:54 PM

Last Updated date:04/25/2003

Last Updated time:08:00:54 PM

Freemarker Date function Processing "Go"

Related Article

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.