码迷,mamicode.com
首页 > Web开发 > 详细

JSTL标签库中fmt标签,日期,数字的格式化

时间:2015-12-16 19:16:35      阅读:5129      评论:0      收藏:0      [点我收藏+]

标签:

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

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:指定解析格式化日期的时区

 

JSTL标签库中fmt标签,日期,数字的格式化

标签:

原文地址:http://www.cnblogs.com/Ant-soldier/p/5051873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!