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

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

转载于:https://www.cnblogs.com/Ant-soldier/p/5051873.html

JSTL标签库中fmt标签,日期,数字的格式化相关推荐

  1. java中fmt标签库_jsp fmt标签详解

    JSTL标签提供了对国际化(I18N)的支持,它可以根据发出请求的客户端地域的不同来显示不同的语言.同时还提供了格式化数据和日期的方法.实现这些功能需要I18N格式标签库(I18N-capable f ...

  2. Java-Web JSTL标签库、自定义标签库和MVC设计模式

    目录 一.JSTL标签库 1.什么是JSTL 2.JSTL标签库 3.使用taglib指令导入标签库 4.core标签库常用标签 (1)out和set (2)remove (3)url (4)if ( ...

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

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

  4. JSTL标签库及常用标签

    JSTL标签库及常用标签 一.JSTL概述 JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用起来非常方便,它与JSP动作标签一样,只不过它不是 ...

  5. 学会怎样使用Jsp 内置标签、jstl标签库及自定义标签

    学习jsp不得不学习jsp标签,一般来说,对于一个jsp开发者,可以理解为jsp页面中出现的java代码越少,对jsp的掌握就越好,而替换掉java代码的重要方式就是使用jsp标签.  jsp标签的分 ...

  6. JSTL中fmt标签详解

    2019独角兽企业重金招聘Python工程师标准>>> 一:JSTL格式化标签又称为I18N标签库,主要用来编写国际化的WEB应用,使用此功能可以对一个特定的语言请求做出合适的处理. ...

  7. JSTL使用总结(2) fmt标签库和fn标签库

    五.fmt标签库 此标签库的作用是进行国际化操作,比如时间格式.数字格式的转换.地区的转换等: 1.国际化知识补充 zh_CN   中国 en_US 美国 fr_FR 法国 2.<fmt:set ...

  8. jstl中Core标签库c:out标签的escapeXml属性

    jstl中<c:out>标签的escapeXml属性讲解 例1: <c:out vlaue="AAA"> <p>BBB</p> &l ...

  9. Web---JSTL(Java标准标签库)-Core核心标签库、I18N国际化、函数库

    前面为JSTL中的常用EL函数,后面的为具体演示实例! JSTL简介: JSTL(Java Standard Tag Library) –Java标准标签库. SUN公司制定的一套标准标签库的规范. ...

最新文章

  1. mongodb php 安装配置,MongoDB 基本安装配置
  2. 无人机飞控开发平台培训理论课程——MSP430最小系统
  3. pandas基础(part2)--DataFrame
  4. vue render函数
  5. PID控制器开发笔记之二:积分分离PID控制器的实现
  6. 初识 Asp.Net内置对象之Request对象
  7. Unity Editor自制工具(1)--“Editor目录栏按钮+全局搜索方法+自制Editor窗口”实现搜索与删除场景中任意名称游戏物体
  8. java遮罩层_页面遮罩层 - javaalex的个人空间 - OSCHINA - 中文开源技术交流社区
  9. 2.3.3 Zero Sum 和为零(DFS)
  10. XAMARIN运行IPHONE模拟器
  11. 磁盘显示无法访问数据错误循环冗余检查的资料恢复法子
  12. 分布式理论分布式ID生成大全
  13. 公开课教学反思 计算机,《百数表》公开课教学反思
  14. Prometheus 部署告警对接 QQ 邮箱
  15. videojs进度条始终为零
  16. 三维计算机视觉(五)--特征描述子
  17. 使用STM32L053探索板上的IDD电流检测功能
  18. QQ玩一玩(轻游戏)开发环境搭建与调试
  19. JS: 火星坐标gcj02、百度坐标bd09II、WGS84坐标相互转换及墨卡托转经纬度百度墨卡托bd09mc
  20. 物联卡买了还没使用就已经被注销,这种情况你遇到过吗?

热门文章

  1. ubuntu18.04安装python3_在 Ubuntu 18.04 上安装 Python 3.7
  2. centos 启动一个redis_基于prometheus+grafana体系监控redis缓存服务
  3. Chapter7-12_Controllable Chatbot
  4. Paddle 使用预训练模型 实现快递单信息抽取
  5. 天池 在线编程 矩阵还原(前缀和)
  6. LeetCode 223. 矩形面积
  7. LeetCode 1016. 子串能表示从 1 到 N 数字的二进制串(bitset)
  8. pcb外观维修_「维修案例」泰克AFG3021函数任意波形发生器故障维修
  9. 获取数据 - 下载附件解压附件 - Python代码
  10. 【社招/实习】百度大搜索招聘NLP、搜索方向算法工程师!