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

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: 可以是time,date或both。控制是否只生成时间,只生成日期,或者时间日期都生成。

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"/>

2004年5月31日   
<fmt:formatDate value="${isoDate}" type="date" dateStyle="full"/>

2004年5月31日 星期一   
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"/>

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

下午11时59分59秒 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标签,日期,数字的格式化的更多相关文章

  1. jsp页面使用EL表达式 使用Jstl标签库中的标签,需要引入jstl.jar和standard.jar

    spring boot 中使用jstl 首先在pom中加入 <dependency> <groupId>jstl</groupId> <artifactId& ...

  2. JSP内置标签 JSP中JavaBean标签 JSP开发模式 EL和JSTL快速入门

    2 JSP内置标签(美化+业务逻辑)   1)为了取代<%%>脚本形式,使用JSP标签/JSP动作,目的:与JSP页面的美化,即JSP面页都是由标签组成,不再有其它的内容   2)JSP内 ...

  3. JSTL标签库之核心标签

    一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心 ...

  4. javaweb学习总结(二十八)——JSTL标签库之核心标签

    一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心 ...

  5. javaWeb学习总结(9)- JSTL标签库之核心标签

    一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心 ...

  6. jsp的标签库和自定义标签

    1.jstl标签库 JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签. ...

  7. ThinkPHP内置标签库原理(Cx标签库)

    任何一个模板引擎的功能都不可能是为你量身定制的,具有一个良好的可扩展 机制也是模板引擎的另外一个考量,Smarty采用的是插件方法来实现扩展,ThinkTemplate由于采用了标签库技术,比Smar ...

  8. 小峰servlet/jsp(7)jstl国际化标签库、sql标签库等

    一.jstl国际化标签库: fmt:setLocale 设定用户所在的区域: fmt:formatDate   对日期进行格式化 fmt:requestEncoding 设置所有的请求编码; fmt: ...

  9. JSTL、JSTL核心标签库——流程处理标签

    JSTL环境 JSTL是另一个标准规范,并非在JSP的规范中,所以必须另外下载JSTL实现. 要使用JSTL标签库,必须在JSP网页上使用taglib指示元素定义前置名称与uri参考.例如,引入JST ...

随机推荐

  1. 010--VS2013 C++ 平面地图贴图

    先准备好地图的小图片: //全局变量HDC mdc;HBITMAP fullmap;const int rows = 8, cols = 8; //-------------------------- ...

  2. JSP基本知识

    JSP基本原理: JSP本质是Servlet(一个特殊的Java类),当用户向指定Servlet发送请求时,Servlet利用输出流动态生成HTML页面.JSP通过在标准的HTML页面中嵌入Java代 ...

  3. hasOwnProperty与isPrototypeOf

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. [转载]ubuntu下如何更改mysql数据存放路径

    http://www.gaojinbo.com/ubuntu%E4%B8%8B%E5%A6%82%E4%BD%95%E6%9B%B4%E6%94%B9mysql%E6%95%B0%E6%8D%AE%E ...

  5. MySQL 分组

    MySQL GROUP BY 语句 GROUP BY 语句根据一个或多个列对结果集进行分组. 在分组的列上我们可以使用 COUNT, SUM, AVG,等函数. GROUP BY 语法 SELECT ...

  6. 关于myeclipse代码提示的一些问题

    默认是  .xxx  输入点提示,要写注释 @xxx的时候怎么输入@后面有代码提示呢? Auto activation delay 是代码提示出现的速度  下面一行是出现代码提示的条件 我们在.后面加 ...

  7. 【BZOJ】【3473】字符串

    后缀数组 Orz zyf 神题不会做啊,先坑着吧……sigh //BZOJ 3473 #include<vector> #include<cstdio> #include< ...

  8. 【BZOJ】【1061】【NOI2008】志愿者招募

    网络流/费用流 OrzOrzOrz,这题太神了不会捉. 题解:https://www.byvoid.com/blog/noi-2008-employee/ 这道题正确的解法是构造网络,求网络最小费用最 ...

  9. JS数组2(冒泡排列、数组里面查找数据)

    数组 一.冒泡排列 对数组attr = [1,8,6,4,5,3,7,2,9]进行由大到小排列,用冒泡排列的方法排列时,会对数组进行比较互换.如果前一个数字较大,这2个元素排列方式不变,如果后一个元素 ...

  10. myeclipse 反编译插件 jad 安装

    1.  准备工作 下载jad.exe文件:http://www.varaneckas.com/sites/default/files/jad/jad158g.win.zip 下载jadeclipse插 ...