Use of jsp tags formatNumber, formatDate, parseNumber, parseDate

To refer to the relevant tags in this article, you need to refer to the corresponding tag library

<%--Import formatted action label Library--%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Format action label

1. Reference to formatNumber tag

formatNumber tag
Syntax format
<fmt:formatNumber
value='<string>'
type ='<string>'
var = '<string>'
scope = "<string>"
/>
Common attributes:
Valuethe value to format
Type the type to format
percent percentage type
Currency currency type
var restricted variable name is used to receive formatted results
scope var attribute range (page|request|session|application)

Note: 1. If the var attribute is used, the label will not output the result, which needs to be obtained through the el expression
2. The default value of type {type} is number
Example code:
<fmt:formatNumber value="10" type="number" var="num" />${num} <br>
<fmt:formatNumber value="1" type="percent"/><br>
<fmt:formatNumber value="10" type="currency" /><br>
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="10" type="currency"/><br>

 

 

2. Reference to formatDate tag

formatDate tag
Converts Date data into a string in the specified format
Syntax format:
<fmt:formatDate
Value = "< string >" date to be formatted
Type = "< string >" formatted type date date date date type year month day time time type hour minute second both time date type
Datestyle = "< string >" date format: FULL LONG MEDIUM SHORT DEFAULT
Timestyle = "< string >" time format: FULL LONG MEDIUM SHORT DEFAULT
Pattern = "< string >" custom mode y M d H m s
Timezone = "< string >" displays the time zone of the date
Var = "< string >" the variable name that stores the formatted date
Scope = "< string >" stores the range of formatted log variables
/>
Example code:
  
<%--format date--%>
    <%
        request.setAttribute("myDate",new Date());
    %>
    ${myDate}<br>
    <fmt:formatDate value="${myDate}"/><br>
    <fmt:formatDate value="${myDate}" type="date"/> <br>
    <fmt:formatDate value="${myDate}" type="time"/> <br>
    <fmt:formatDate value="${myDate}" type="both"/> <br>
    <fmt:formatDate value="${myDate}" type="both" dateStyle="FULL"/> <br>
    <fmt:formatDate value="${myDate}" type="both" timeStyle="short"/><br>
    <fmt:formatDate value="${myDate}" pattern="yyyy-MM-dd"/><br>
    <hr>

 

 

3. Reference to parseNumber tag

parseNumber tag
Converts a string in the specified format to numeric
Syntax format:
<fmt:parseNumber
value="<string>"
type="<string>"
var="<string>"
scope="<string>"
/>

Example code:
  
    <fmt:setLocale value="zh_CN"/>
    <fmt:parseNumber value="100"/> <br>
    <fmt:parseNumber value="100" type="number"/> <br>
    <fmt:parseNumber value="100%" type="percent" /> <br>
    <fmt:parseNumber value="¥10.00" type="currency"/><br>

 

 

Error record here:

code:

 <fmt:parseNumber value="100"/> <br>
    <fmt:parseNumber value="100" type="number"/> <br>
    <fmt:parseNumber value="100%" type="percent" /> <br>
    <fmt:parseNumber value="¥10.00" type="currency"/><br>

Error display:

Cause of error:

The display format here is still the U.S. display format set by the above agent < FMT: setlocale value = "en_us" / > so the '¥' character cannot be recognized,

Therefore, change the display format to < FMT: setlocale value = "zh_cn" / > to correct the error

4. Reference to parseDate tag

parseDate tag
Converts a Date string to a Date string
<fmt:parseDate
Value = "< string >" date to be formatted
Type = "< string >" formatted type date date date date type year month day time time type hour minute second both time date type
Datestyle = "< string >" date format: FULL LONG MEDIUM SHORT DEFAULT
Timestyle = "< string >" time format: FULL LONG MEDIUM SHORT DEFAULT
Pattern = "< string >" custom mode y M d H m s
Timezone = "< string >" displays the time zone of the date
Var = "< string >" the variable name that stores the formatted date
Scope = "< string >" stores the range of formatted log variables
/>
Example code:
  
<fmt:parseDate value="2021-01-11" type="date"/> <br>
<fmt:parseDate value="2021/01/11" pattern="yyyy/MM/dd" /><br>

 

 

Keywords: Java

Added by saviiour on Wed, 05 Jan 2022 05:32:12 +0200