Jstl the FMT tag in the tag library, date, number formatting

Source: Internet
Author: User
Tags time zones locale locale setting java format

First introduce the date formatting: (not too many OH) Jstl formatted date (localized)

Similar to digital and currency formatting, the localized environment also affects how dates and times are generated.

<%@ page pageencoding= "UTF-8"%>
<%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix= "FMT" uri= "Http://java.sun.com/jsp/jstl/fmt"%>

<title>date formatting</title>
<body>
<fmt:timezone value= "EST" >
<jsp:usebean id= "CurrentTime" class= "Java.util.Date"/>

<fmt:setlocale value= "en_GB"/>
<fmt:formatdate type= "Both" datestyle= "full" timestyle= "full" value= "${currenttime}"/><br/>

<fmt:setlocale value= "en_US"/>
<fmt:formatdate type= "Both" datestyle= "full" timestyle= "full" value= "${currenttime}"/><br/>

<fmt:setlocale value= "Fr_fr"/>
<fmt:formatdate type= "Both" datestyle= "full" timestyle= "full" value= "${currenttime}"/><br/>

<fmt:setlocale value= "Ja_jp"/>
<fmt:formatdate type= "Both" datestyle= "full" timestyle= "full" value= "${currenttime}"/><br/>

<fmt:setlocale value= "Ko_kr"/>
<fmt:formatdate type= "Both" datestyle= "full" timestyle= "full" value= "${currenttime}"/><br/>

<fmt:setlocale value= "Es_es"/>
<fmt:formatdate type= "Both" datestyle= "full" timestyle= "full" value= "${currenttime}"/><br/>


<fmt:setlocale value= "Ar_eg"/>
<fmt:formatdate type= "Both" datestyle= "full" timestyle= "full" value= "${currenttime}"/><br/>

</fmt:timeZone>
</body>

properties of the <fmt:formatDate> action

Type: can be time,date , or both. Controls whether only the time is generated, the date is generated, or the time date is generated.

Datestyle: can be short , medium, long or full (default). Controls the specific format used for the print date.

Timestyle: can be short , medium, long or full (default). Controls the specific format used for printing time.

Value: This is a java.util.Date type that is used to generate the date and time.

jstl Formatting date labels

JSP Standard Tag Libraries
Formatting and internationalization
The form input parameters, ' date ' and ' isodate ', is url-encoded in the link leading to the page. ' Isodate ' is formatted according-to-the-ISO8601 standard.
Formatting of numbers and dates are based on the browser ' s locale setting. Formatting if you switch the default language setting from 中文版 to French or German, for example. (The browser needs to be restarted, too.)

Library Import and parameter capturing:

<%@ taglib prefix= "C" uri= "Http://java.sun.com/jstl/core"%>
<%@ taglib prefix= "FMT" uri= "Http://java.sun.com/jstl/fmt"%>

<fmt:parsedate value= "${param.date}" var= "date" pattern= "yyyy/mm/dd:hh:mm:ss>
<fmt:parsedate value= "${param.isodate}" var= "Isodate" pattern= "YyyyMMdd ' T ' Hhmmss" >

The input parameters must match the patterns, or the JSP would thrown an exception. This page does no error handling.

Input Parameters:
date:2004/04/01:13:30:00 Java Format:thu Apr 13:30:00 CST 2004
isodate:20040531t235959 Java Format:mon 23:59:59 CDT 2004

Dates
Tag Output
Attribute:value; Required. Tag has no body. (Typedefault isdate)
<fmt:formatdate value= "${date}" type= "both"/>

2004-4-1 13:30:00
<fmt:formatdate value= "${isodate}" type= "both"/>

2004-5-31 23:59:59
Attribute:type; Optional. Indicates to Print:date, time, or both.
<fmt:formatdate value= "${date}" type= "date"/>

2004-4-1
<fmt:formatdate value= "${isodate}" type= "Time"/>

23:59:59
Attribute:datestyle; Optional. varies the date format. (The default isMedium)
<fmt:formatdate value= "${isodate}" type= "date" datestyle= "Default"/>

2004-5-31
<fmt:formatdate value= "${isodate}" type= "date" datestyle= "short"/>

04-5-31
<fmt:formatdate value= "${isodate}" type= "date" datestyle= "Medium"/>

2004-5-31
<fmt:formatdate value= "${isodate}" type= "date" datestyle= "Long"/>

2004years5Month toDay   
<fmt:formatdate value= "${isodate}" type= "date" datestyle= "full"/>

2004years5Month toDay Monday   
Attribute:timestyle; Optional. varies the time format. (The default isMedium)
<fmt:formatdate value= "${isodate}" type= "Time" timestyle= "Default"/>

23:59:59
<fmt:formatdate value= "${isodate}" type= "Time" timestyle= "short"/>

Afternoon11:59
<fmt:formatdate value= "${isodate}" type= "Time" timestyle= "Medium"/>

23:59:59
<fmt:formatdate value= "${isodate}" type= "Time" timestyle= "Long"/>

Afternoon Onewhen -points -seconds   
<fmt:formatdate value= "${isodate}" type= "Time" timestyle= "full"/>

Afternoon Onewhen -points -secondsCDT
Attribute:pattern; Optional. Inidcates Date/time Custom Patterns.
<fmt:formatdate value= "${date}" type= "both" pattern= "eeee, MMMM D, yyyy HH:mm:ss Z"/>

Thursday, April1, 2004 13:30:00-0600
<fmt:formatdate value= "${isodate}" type= "both" pattern= "D MMM yy, h:m:s a zzzz/>

formatting of numbers in Jstl

Display: $12.00
<fmt:formatnumber value= "type=" Currency "pattern=" $.00 "/> <br/>
Display: $12.0
<fmt:formatnumber value= "type=" Currency "pattern=" $.# "/> <br/>
¥12.0
<fmt:formatnumber value= "type=" Currency "pattern=" ¥.00 "/> <br/>
12.00 USD
<fmt:formatnumber value= "type=" Currency "pattern=" #0.00 USD "/> <br/>
¥12.00
<fmt:formatnumber value= "" type= "Currency"/> (the symbol for that currency is related to the local setting of the current Web server) <br/>

123456.79
<fmt:formatnumber value= "123456.7891" pattern= "#0. xx"/> <br/>
123,456.79
<fmt:formatnumber value= "123456.7891" pattern= "#, #00. xx"/> <br/>
.79
<fmt:formatnumber value= "0.7891" pattern= "#.00"/> <br/>
12.34%
<fmt:formatnumber value= "0.1234" type= "percent" pattern= "#0.00%"/><br/>
1,200%
<fmt:formatnumber value= "type=" percent "/><br/>
1200%
<fmt:formatnumber value= "type=" percent "pattern=" #0.00% "/><br/>"
------------------------------------------------------------------------------
Java formatted output:
DecimalFormat df = new DecimalFormat ("format");
String FMT =df.format (double);
Symbolic meaning
01 Digits
# one digit, leading 0 and rear-end 0 not shown
. decimal point Split position
, the position of the group separator
-Negative prefix
% is used 100 times, and the percent sign is displayed
Any other symbol that includes the specified symbol in the output string

instructions for use of other FMT tags:

Formatting tag library: is used to do international formatting in the JSP page action
are divided into two categories, namely:
International core Tags:<fmt:setlocale>, <fmt:bundle>, <fmt:setBundle>, <fmt:message>, <fmt:param>, <fmt:requestEncoding>
Formatting tags:<fmt:timezone>, <fmt:setTimeZone>, <fmt:formatNumber>, <fmt:parseNumber>, <fmt: Formatdate>, <fmt:parseDate>

1.<fmt:setlocale> Tags: for setting up a localized environment
Property Description
Value:locale the specified environment, which can be an instance of the Java.util.Locale or String type
Scope:locale the scope of the environment variable (optional)
Such as:
Set local environment to traditional Chinese
<fmt:setlocale value= "Zh_tw"/>
Set local environment to Simplified Chinese
<fmt:setlocale value= "Zh_cn"/>

2.<fmt:requestencoding> Tags: used to set character encoding for requests
It has only one property, value, in which the character encoding can be defined.
Such as:
<fmt:requestencoding value= "GB2312"/>

3.<fmt:bundle>, <fmt:setBundle> Tags: data sources for resource profiles
The 3.1<fmt:bundle> tag binds a resource profile to the display in its tag body
Property Description
basename: Specify the resource profile, just specify the file name without the extension
Prefix: Pre-keyword
Such as:
The data configured in the resource file is:
Label.backcolor= #FFF
Label.fontcolor= #000
Then, you can get the BackColor and FontColor values for the label as follows:
<fmt:bundle basename= "Myresourse" prefix= "label." >
<fmt:message key= "BackColor"/>
<fmt:message key= "FontColor"/>
</fmt:bundle>

The 3.2<fmt:setbundle> tag allows you to save a resource profile as a variable, which you can then work on according to the variable.
Attribute description, property shared by two sets of labels
Var:<fmt:setbundle> Unique property for saving a resource profile as a variable
Scope: The scope of the variable
Such as:
Find a resource profile named Applicationmessage_zh_cn.properties to be shown as Resource binding
<fmt:setbundle basename= "Applicationmessage" var= "Applicationbundle"/>


4.<fmt:message> Tags: used to display resource profile information (the resource file must follow the following format:

1. The extension must be properties,2. The contents of the file must be in the format of key = value; 3. File to be placed in the web-inf/classes directory)
Property Description
Key: The "key" designation for the resource profile
Bundle: If you use <fmt:setBundle> to save a resource profile, this property can be found from a saved resource profile
var: Save display information as a variable
Scope: The scope of the variable
Such as:
1) The "Applicationmessage" resource profile is assigned to the variable "Applicationbundle" with the <fmt:setBundle> tag
Use the <fmt:message> tab to display information about "key" as "PassWord" in the resource profile saved by the <fmt:setBundle> tag

<fmt:setbundle basename= "Applicationmessage" var= "Applicationbundle"/>
<fmt:message key= "PassWord" bundle= "${applicationbundle}"/>

2) the "Applicationallmessage" resource profile defined with the <fmt:bundle> tag acts on the display in its label body
Use the <fmt:message> tab to display the information "key" is "UserName" in the "Applicationallmessage" Resource configuration file

<fmt:bundle basename= "Applicationallmessage" >
<fmt:message key= "UserName"/>
</fmt:bundle>

5.<fmt:param Tags: for parameter passing
The <fmt:param> tag should be in the <fmt:message> tab and will provide the parameter value for the message label. It has only one property value
For example: In the Myresourse.properties file, there is an index value as follows (where {0} represents a placeholder):
STR2=HI,{0}
Then, use the <fmt:param> tag to pass in the values as follows:
<fmt:bundle basename= "Myresourse" >
<fmt:message key= "STR2" >
<fmt:param value= "Zhang San"/>
</fmt:message>
</fmt:bundle>
You can also specify the type of the parameter in the resource file:
For example: In the Myresourse.properties file, there is an index value as follows:
Str3={0,date}
Then, use the <fmt:param> tag to pass in the values as follows:
<% Request.setattribute ("Now", New Date ()); %>
<fmt:bundle basename= "Myresourse" >
<fmt:message key= "STR3" >
<fmt:param value= "${now}"/>
</fmt:message>
</fmt:bundle>


6.<fmt:timezone>, <fmt:setTimeZone> Tags: for setting time zones
<fmt:timeZone> tags will allow work in its label body to use this time zone setting
The <fmt:setTimeZone> tag allows you to save the time zone setting as a variable, after which the work can be done according to the variable
Property Description
Value: Settings for the time zone
Var:<fmt:settimezone> Unique property for saving the time zone as a variable
Scope: The scope of the variable


7.<fmt:formatnumber> Tags: for formatting numbers
Property Description
Value: A formatted number that can be an instance of type String or Java.lang.Number
Type: Formatted types, possible values include: currency (currency), number (numeric), and percent (percent)
Pattern: Format mode
var: result save variable
Scope: The scope of the variable
Maxintegerdigits: Specifies the maximum value of the formatted result
Minintegerdigits: Specifies the minimum value of the formatted result
Maxfractiondigits: Specifies the maximum value of the formatted result, with decimals
Minfractiondigits: Specifies the minimum value for the formatted result, with decimals

Such as:
The results will be saved in the "money" variable, and the local currency format will be displayed according to the locale environment
<fmt:formatnumber value= "1000.888" type= "Currency" var= "Money"/>


8.<fmt:parsenumber> Tags: used to parse numbers
Property Description
Value: The string to be parsed
Type: Parsing the formatted types
Pattern: Parsing Formatting patterns
var: result save variable, type Java.lang.Number
Scope: The scope of the variable
Parselocale: Parses a string in a localized form, and the content of the property should be an instance of a string or Java.util.Locale type

Such as:
Convert "15%" to a number
<fmt:parsenumber value= "15%" type= "percent" var= "num"/>


9.<fmt:formatdate> Tags: for formatting dates
Property Description
Value: The formatted date, and the content of the attribute should be an instance of the Java.util.Date type
Type: Formatted types
Pattern: Format mode
var: result save variable
Scope: The scope of the variable
TimeZone: Specifies the time zone for the formatted date


10.<fmt:parsedate> Tags: for resolving dates
Property Description
Value: The string to be parsed
Type: Parsing the formatted types
Pattern: Parsing Formatting patterns
var: result save variable, type Java.lang.Date
Scope: The scope of the variable
Parselocale: Parses a string in a localized form, with the contents of the property being an instance of type string or Java.util.Locale
TimeZone: Specifies the time zone to parse the formatted date

Jstl the FMT tag in the tag library, date, number formatting

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.