jstl格式化

首先介紹日期的格式化:(不要嫌多哦)

JSTL格式化日期(本地化)

類似於數字和貨幣格式化,本地化環境還會影響生成日期和時間的方式。

<%@ 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" %>

<html>
<head>
<title>Date Formatting</title>
</head>
<body>
<h1>Date Formatting and locale</h1>
<fmt:timeZone value="EST">
<jsp:useBean id="currentTime" class="java.util.Date"/>

<h3>English, Great Britain</h3>
<fmt:setLocale value="en_GB" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>

<h3>English, USA</h3>
<fmt:setLocale value="en_US" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>

<h3>French, France</h3>
<fmt:setLocale value="fr_FR" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>

<h3>Japanese, Japan</h3>
<fmt:setLocale value="ja_JP" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>

<h3>Korean, Korea</h3>
<fmt:setLocale value="ko_KR" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>

<h3>Spanish, Spain</h3>
<fmt:setLocale value="es_ES" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>


<h3>Arabic, Egypt</h3>
<fmt:setLocale value="ar_EG" />
<fmt:formatDate type="both" dateStyle="full" timeStyle="full" value="${currentTime}" /><br/>

</fmt:timeZone>
</body>
</html>

<fmt:formatDate>動作的屬性

type: 可以是timedateboth。控制是否只生成時間,只生成日期,或者時間日期都生成。

dateStyle: 可以是short, medium, long 或 full(default)。控制打印日期使用的具體格式。

timeStyle: 可以是short, medium, long 或 full(default)。控制打印時間使用的具體格式。

value: 這是一個java.util.Date 類型的值,用於生成日期和時間。

jstl格式化日期標籤

JSP Standard Tag Libraries 
Formatting and Internationalization 
Two form input parameters, ‘date‘ and ‘isoDate‘, are URL-encoded in the link leading to this page. ‘isoDate‘ is formatted according to the ISO8601 standard. 
Formatting of numbers and dates is based on the browser‘s locale setting. Formatting will change if you switch the default language setting from English 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 will thrown an exception. This page does no error handling. 

Input parameters: 
Date:     2004/04/01:13:30:00    Java format: Thu Apr 01 13:30:00 CST 2004 
isoDate: 20040531T235959        Java format: Mon May 31 23:59:59 CDT 2004 

Dates 
Tag Output 
Attribute: value; required. Tag has no body. (type默認是date)
<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 what 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. (默認是medium
<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"/> 

2004531   
<fmt:formatDate value="${isoDate}" type="date" dateStyle="full"/> 

2004531日 星期一   
Attribute: timeStyle; optional. Varies the time format. (默認是medium
<fmt:formatDate value="${isoDate}" type="time" timeStyle="default"/> 

23:59:59   
<fmt:formatDate value="${isoDate}" type="time" timeStyle="short"/> 

下午11:59   
<fmt:formatDate value="${isoDate}" type="time" timeStyle="medium"/> 

23:59:59   
<fmt:formatDate value="${isoDate}" type="time" timeStyle="long"/> 

下午115959   
<fmt:formatDate value="${isoDate}" type="time" timeStyle="full"/> 

下午115959秒 CDT   
Attribute: pattern; optional. Inidcates date/time custom patterns. 
<fmt:formatDate value="${date}" type="both" pattern="EEEE, MMMM d, yyyy HH:mm:ss Z"/> 

星期四四月 1, 2004 13:30:00 -0600   
<fmt:formatDate value="${isoDate}" type="both" pattern="d MMM yy, h:m:s a zzzz/> 

JSTL中數字的格式化

 顯示:$12.00
<fmt:formatNumber value="12" type="currency" pattern="$.00"/>  <br/>
顯示:$12.0
<fmt:formatNumber value="12" type="currency" pattern="$.#"/> <br/> 
¥12.0 
<fmt:formatNumber value="12" type="currency" pattern="¥.00"/> <br/>
12.00元
<fmt:formatNumber value="12" type="currency" pattern="#0.00元"/> <br/>
¥12.00 
<fmt:formatNumber value="12" type="currency"/>  (那個貨幣的符號和當前web服務器的 local 設定有關)<br/>

123456.79
<fmt:formatNumber value="123456.7891" pattern="#0.00"/>  <br/>
123,456.79
<fmt:formatNumber value="123456.7891" pattern="#,#00.00"/> <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="12" type="percent"  /><br/>
1200.00%
<fmt:formatNumber value="12" type="percent" pattern="#0.00%"/><br/>
------------------------------------------------------------------------------
java格式化輸出:
DecimalFormat df = new DecimalFormat("格式");
String fmt =df.format(double);
符號                  意義
0                     一個數位
#                     一個數位,前導零和追尾零不顯示
.                      小數點分割位置
,                     組分隔符的位置
-                      負數前綴
%                    用100乘,並顯示百分號
其他任何符號    在輸出字符串中包括指定符號

其他fmt標籤的使用說明:

formatting標籤庫:就是用於在 JSP 頁面中做國際化格式化的動作
分爲了兩類,分別是:                                                                                                  
國際化核心標籤:<fmt:setLocale>、<fmt:bundle>、<fmt:setBundle>、<fmt:message>、<fmt:param>、<fmt:requestEncoding>
格式化標籤:<fmt:timeZone>、<fmt:setTimeZone>、<fmt:formatNumber>、<fmt:parseNumber>、<fmt:formatDate>、<fmt:parseDate>

1.<fmt:setLocale>標籤:用於設置本地化環境
屬性描述 
value:Locale 環境的指定,可以是 java.util.Locale 或 String 類型的實例 
scope:Locale 環境變量的作用範圍(可選) 
如:
    設置本地環境爲繁體中文
    <fmt:setLocale value="zh_TW"/>
    設置本地環境爲簡體中文
    <fmt:setLocale value="zh_CN"/>

2.<fmt:requestEncoding>標籤:用於爲請求設置字符編碼
它只有一個屬性 value ,在該屬性中可以定義字符編碼。 
如:
    <fmt:requestEncoding value="GB2312"/>

3.<fmt:bundle> 、 <fmt:setBundle> 標籤:用於資源配置文件的數據來源
3.1<fmt:bundle> 標籤將資源配置文件綁定於它標籤體中的顯示
屬性描述
basename:資源配置文件的指定,只需要指定文件名而無須擴展名
prefix:前置關鍵字
如:
資源文件中配置的數據爲:
label.backcolor=#FFF
label.fontcolor=#000
則,可以用如下方法取得label的backcolor和fontcolor值:
    <fmt:bundle basename="MyResourse" prefix="label."> 
        <fmt:message key="backcolor" />
        <fmt:message key="fontcolor" />
    </fmt:bundle>

3.2<fmt:setBundle> 標籤則允許將資源配置文件保存爲一個變量,在之後的工作可以根據該變量來進行
屬性描述 ,二組標籤共有的屬性 
var:<fmt:setBundle> 獨有的屬性,用於保存資源配置文件爲一個變量 
scope:變量的作用範圍 
如:
    查找一個名爲 applicationMessage_zh_CN.properties 的資源配置文件,來作爲顯示的 Resource 綁定
    <fmt:setBundle basename="applicationMessage" var="applicationBundle"/> 


4.<fmt:message> 標籤:用於顯示資源配置文件信息(該資源文件必須遵循如下格式:

1.擴展名必須爲properties,2.文件的內容必須依照key = value的格式;3.文件要放到WEB-INF/classes目錄下)
屬性描述 
key:資源配置文件的“鍵”指定 
bundle:若使用 <fmt:setBundle> 保存了資源配置文件,該屬性就可以從保存的資源配置文件中進行查找 
var:將顯示信息保存爲一個變量 
scope:變量的作用範圍 
如:
1)用<fmt:setBundle>標籤將"applicationMessage"資源配置文件被賦於了變量"applicationBundle"
    用<fmt:message>標籤顯示由<fmt:setBundle>標籤保存的資源配置文件中"鍵"爲"passWord"的信息
    
        <fmt:setBundle basename="applicationMessage" var="applicationBundle"/> 
        <fmt:message key="passWord" bundle="${applicationBundle}" />

2)用<fmt:bundle>標籤定義的"applicationAllMessage"資源配置文件作用於其標籤體內的顯示
    用<fmt:message>標籤顯示"applicationAllMessage"資源配置文件中"鍵"爲"userName"的信息

        <fmt:bundle basename="applicationAllMessage"> 
            <fmt:message key="userName" />
        </fmt:bundle>

5.<fmt:param 標籤:用於參數傳遞
<fmt:param>標籤應該位於 <fmt:message> 標籤內,將爲該消息標籤提供參數值。它只有一個屬性value 
如:在MyResourse.properties文件中,有一個索引值如下(其中,{0}代表佔位符):
Str2=Hi,{0} 
則,使用<fmt:param>標籤傳入值如下:
    <fmt:bundle basename="MyResourse"> 
        <fmt:message key="Str2">
            <fmt:param value="張三" />
        </fmt:message>
    </fmt:bundle>
也可以在資源文件中指定參數的類型:
如:在MyResourse.properties文件中,有一個索引值如下:
Str3={0,date}
則,使用<fmt:param>標籤傳入值如下:
    <% 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>標籤:用於設定時區
<fmt:timeZone> 標籤將使得在其標籤體內的工作可以使用該時區設置
<fmt:setTimeZone> 標籤則允許將時區設置保存爲一個變量,在之後的工作可以根據該變量來進行
屬性描述 
value:時區的設置 
var:<fmt:setTimeZone> 獨有的屬性,用於保存時區爲一個變量 
scope:變量的作用範圍 


7.<fmt:formatNumber>標籤:用於格式化數字
屬性描述 
value:格式化的數字,該數值可以是 String 類型或 java.lang.Number 類型的實例 
type:格式化的類型,可能值包括:currency(貨幣)、number(數字)和percent(百分比)
pattern:格式化模式 
var:結果保存變量 
scope:變量的作用範圍 
maxIntegerDigits:指定格式化結果的最大值 
minIntegerDigits:指定格式化結果的最小值 
maxFractionDigits:指定格式化結果的最大值,帶小數 
minFractionDigits:指定格式化結果的最小值,帶小數 

如:
    結果將被保存在“ money ”變量中,將根據 Locale 環境顯示當地的貨幣格式
        <fmt:formatNumber value="1000.888" type="currency" var="money"/>


8.<fmt:parseNumber> 標籤:用於解析數字
屬性描述 
value:將被解析的字符串 
type:解析格式化的類型 
pattern:解析格式化模式 
var:結果保存變量,類型爲 java.lang.Number 
scope:變量的作用範圍 
parseLocale:以本地化的形式來解析字符串,該屬性的內容應爲 String 或 java.util.Locale 類型的實例 

如:
    將"15%"轉換爲數字
        <fmt:parseNumber value="15%" type="percent" var="num"/> 


9.<fmt:formatDate>標籤:用於格式化日期
屬性描述
value:格式化的日期,該屬性的內容應該是 java.util.Date 類型的實例
type:格式化的類型
pattern:格式化模式
var:結果保存變量
scope:變量的作用範圍
timeZone:指定格式化日期的時區


10.<fmt:parseDate>標籤:用於解析日期
屬性描述 
value:將被解析的字符串 
type:解析格式化的類型 
pattern:解析格式化模式 
var:結果保存變量,類型爲 java.lang.Date 
scope:變量的作用範圍 
parseLocale:以本地化的形式來解析字符串,該屬性的內容爲 String 或 java.util.Locale 類型的實例 
timeZone:指定解析格式化日期的時區

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章