jsp页面格式化数字或时间
转载自: http://blog.csdn.net/hakunamatata2008/archive/2011/01/21/6156203.aspx

Tags
fmt:requestEncoding
fmt:setLocale
fmt:timeZone
fmt:setTimeZone
fmt:bundle
fmt:setBundle
fmt:message
fmt:param
fmt:formatNumber
fmt:parseNumber
fmt:formatDate
fmt:parseDate 

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

jstl fmt 函数大全

主要功能格式化

日期格式(2008年5月5日22点00分23秒)

<fmt:formatDate value="<%=new Date() %>" pattern="yyyy年MM月dd日HH点mm分ss秒" />

保留两位小数

<fmt:formatNumber value="123.123456789" pattern="0.00"/>

格式数字(45,678.234)

<fmt:formatNumber type="number" value="45678.2345" />

格式百分比(23%)
<fmt:formatNumber type="percent" value="0.2345" />
<fmt:formatNumber  value="${item.DD_NUM/item.TOL_NUM}" type="number" pattern="0.00%" /> 

其他

<fmt:bundle>:资源绑定。除了以前提到过的在web.xml中声明以外,还可以利用此标签。

例<fmt:bundle basename="message"></fmt:bundle>

<fmt:setLocale>:设置locale,主要是用于这种情况,一个中国人在国外,locale是en_US,但想用中文显示。

例:<fmt:setLocal value="zh_CN"/>

<fmt:message>:输出properties文件中的指定内容。

例<fmt:message key="user"/>

<fmt:formatNumber type="number">格式化普通数字
<fmt:formatNumber type="percent">格式化百分比

三种数字类型参数:currency,number,percent

<fmt:parseNumber var="i" type="number" value="45678.2345" />
<c:out value="${i}" escapeXml="false" /> 分析出数字

<fmt:requestEncoding value="GB18030"/> 格式化文本编码

<fmt:formatDate value="${date}" type="both" timeStyle="long" dateStyle="long" />
type="both" 输入日期也同时输出具体时间
timeStyle="long" 时间以“长”格式输出 差别:下午02时06分59秒 与 14:06:59
dateStyle="long" 日期以“长”格式输出 差别:2006年9月7日 与 2006-9-7

四种长短参数:long,short,medium,full

<fmt:timeZone value="${timezone}"/> 时区偏移,与上面可配合使用:
<fmt:formatDate value="${d}" timeZone="${zn}" type="both" />

<fmt:parseDate var="i" type="date" value="2006-12-11" />
<c:out value="${i}" escapeXml="false" /> 分析出时间

具体例子:

1)导入jstl 包,加载ftm标签

首先将jstl的jar包放入类库中,使用1.2版本

其次在jsp文件中引入所需要的 标记库,对于 ftm 标签,如下:

view plaincopy to clipboardprint?

    <%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %> 

2)输出 .properties 文件中的信息

view plaincopy to clipboardprint?

    <fmt:bundle basename="fmt">
        test value:<fmt:message key="test" />
    </fmt:bundle> 

其中 <fmt:bundle basename="fmt"> 指定了资源文件的位置,例如: fmt 表示类根路径下的 fmt.properties 文件,my.fmt 表示 包my下的ftm.properties文件;

<fmt:message key="test" />表示读取 key为test的值,并输出;

3)给出1个例子,包含许多标签的使用

fmt.jsp:

view plaincopy to clipboardprint?

    <%@ page language="java" contentType="text/html; charset=utf-8" 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" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
        -->
        <mce:style type="text/css"><!--
            body {background-color: black;color: white;}
            span {text-align: center;color: green;background-color: yellow;}
            .notice {color: rgb(250,37,62);}
            hr { background-color: fuchsia; height: 5px;} 

    --></mce:style><style type="text/css" mce_bogus="1">     body {background-color: black;color: white;}
            span {text-align: center;color: green;background-color: yellow;}
            .notice {color: rgb(250,37,62);}
            hr { background-color: fuchsia; height: 5px;}
        </style>
      </head> 

      <body>
        <fmt:bundle basename="jstl.jstl">
            <span>从 .properties 文件中读取最简单的信息输出:</span>
            <fmt:message key="basemsg" />
            <hr/>
            <span>从 .properties 文件中读取带有可填参数的信息,填入参数并输出:</span>
            <fmt:message key="msgwithparam">
                <span class="notice"><fmt:param value="param-1-value" />
                <span class="notice"><fmt:param value="param-2-value" />
            </fmt:message>
            <hr/>
            <span>数字 格式化并输出:</span><br/>
            数字:<fmt:formatNumber value="1234567890" type="number"/><br/>
            <!-- 定制数字格式时,# 表示按照默认格式来, -->
            数字,定制了格式:<fmt:formatNumber value="1234567890" type="number" pattern="#,#00.0#" /><br/>
            货币:<fmt:formatNumber value="35000" type="currency" /><br/>
            百分比:<fmt:formatNumber value="0.317" type="percent" /><br/>
            <hr/>
            <span>格式化日期:</span><br/>
            <jsp:useBean id="now" class="java.util.Date"></jsp:useBean>
            <fmt:formatDate  value="${now}" type="date" /><br/>
            <fmt:formatDate  value="${now}" type="both" dateStyle="long" timeStyle="long" /><br/>
            <fmt:formatDate  value="${now}" type="both" pattern="yyyy.MM.dd HH:mm:ss" /><br/>
            <hr/>
            <span>将字符串转化到正确的数字:<br/>
            忽略第一个不符合数字条件的字符和其之后的所有字符,如果字符串不是以数字开头则报错</span><br/>
            <fmt:parseNumber type="number" >123.02a</fmt:parseNumber><br/>
            <fmt:parseNumber type="number" pattern="#,#00.0#">123</fmt:parseNumber><br/>
            <fmt:parseNumber type="number" pattern="#,#00.0#">123.00a1</fmt:parseNumber><br/>
            <fmt:parseNumber type="number" pattern="#,#00.0#">3saaa</fmt:parseNumber><br/> 

        </fmt:bundle> 

      </body>
    </html> 

jstl 包下的 jstl.properties 文件:

view plaincopy to clipboardprint?

    #for jstl learn
    basemsg=This is a base msg.
    msgwithparam=This is a msg with params:first <font color="red">{0}</font> second <font color="aqua"> {1}</font> .  

另一篇:

国际化格式标签库包括国际化,消息和数字日期格式化:

(1) 国际化:<fmt:setLocale> <fmt::requestEncoding>

如:
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.util.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:set var="todayValue" value="<%=new Date() %>"/>

中文-大陆:<fmt:setLocale value="zh"/>
<fmt:formatDate value="${todayValue}"/><br>
中文-台湾<fmt:setLocale value="zh_tw"/>
<fmt:formatDate value="${todayValue}"/><br>
中文-新加坡<fmt:setLocale value="zh_sg"/>
<fmt:formatDate value="${todayValue}"/><br>
英文:<fmt:setLocale value="en"/>
<fmt:formatDate value="${todayValue}"/>
</body>
</html>

页面输出:

中文-大陆: 2007-12-25
中文-台湾 2007/12/25
中文-新加坡 25-十二月-07
英文: Dec 25, 2007

(2)消息标签:<fmt:bundle> <fmt:message> <fmt:setBundle> <fmt:param>

如:
<%@ page language="java" contentType="text/html; charset=gb2312" import="java.util.*"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>bundle test</title>
</head>
<body>
<fmt:bundle basename="dbconn">
数据库驱动程序名:<fmt:message key="driverName"/><br>
连接字符串:<fmt:message key="connString"/><br>
用户名:<fmt:message key="userName"/><br>
密码:<fmt:message key="password" var="password"/>
     <c:out value="${password}"/><br>
名字:<fmt:message key="name"/><br>
动态提示信息:<fmt:message key="messageTemp"/><br>
</fmt:bundle>

<!-- 修改.properties文件中某个键的动态值 -->
<c:set var="todayTemp" value="<%=new Date() %>"/>
<fmt:setBundle basename="dbconn"/>
动态提示信息:
<fmt:message key="messageTemp">
  <fmt:param>邓子云</fmt:param>
  <fmt:param value="${todayTemp}"></fmt:param>
</fmt:message>

</body>
</html> 

JSP页面格式化数字或时间 基于jstl的的更多相关文章

  1. JSP页面格式化数字或时间 基于struts的

    jsp日期格式化 转自: http://blog.csdn.net/chj225500/article/details/7251552 在直接<s:textfield中也要日期格式化,平时使用日 ...

  2. jsp页面格式化数字或时间

    Tags   fmt:requestEncoding fmt:setLocale fmt:timeZone fmt:setTimeZone fmt:bundle fmt:setBundle fmt:m ...

  3. jsp页面动态获取系统时间

    最近在做练习时碰到了这样一个问题:"读者选择查询图书相应信息,跳转到书目的详细信息界面,当可借阅数量大于零,点击借阅按钮,提示用户借阅成功,并显示归还日期(三个月),否则提示用户该书可借阅数 ...

  4. jsp页面格式化时间 fmt:formatDate格式化日期

    使用fmt函数需在jsp中引入 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" ...

  5. fmt标签格式化数字和时间

    有时候需要格式化输出数字和时间,fmt 标签是个很好用的标签,下面是我做的总结: 在页面的头部加入这个标签 <%@ taglib uri="http://java.sun.com/js ...

  6. JSP页面格式化timestamp时间

    timestamp类型返回并直接取值显示会带小尾巴 偶然发现 <fmt:formatDate value="${order.createTime}" type="b ...

  7. JAVA中String.format的用法 格式化字符串,格式化数字,日期时间格式化,

    1.对整数进行格式化:%[index$][标识][最小宽度]转换方式        我们可以看到,格式化字符串由4部分组成,其中%[index$]的含义我们上面已经讲过,[最小宽度]的含义也很好理解, ...

  8. 在JSP页面中显示动态时间

    源地址:http://blog.csdn.net/aitcax/article/details/41285305 静态时间: 1.页面首部添加 <%@page import="java ...

  9. JSP页面格式化货币金额,千分位

    <fmt:formatNumber value="${值}" pattern="currency"></fmt:formatNumber> ...

随机推荐

  1. LINQ to Entities 中的查询

    MSDN地址:https://msdn.microsoft.com/zh-cn/library/bb399367%28v=vs.100%29.aspx .NET Framework 4 查询是一种从数 ...

  2. 改造继续之eclipse集成tomcat开发spring mvc项目配置一览

    在上一篇的环境配置中,你还只能基于maven开发一个javase的项目,本篇来看如果开发一个web项目,所以还得配置一下tomcat和spring mvc. 一:Tomcat安装 在.net web开 ...

  3. 【转载】webstorm11(注册,激活,破解,码,一起支持正版,最新可用)(2016.11.16更新)

    很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 最近封的厉害仅作测试 选择 License ...

  4. linux shell 之在线文本编辑sed

    sed命令 文件编辑 sed是一种文本编辑命令,通过终端读取文件数据到缓冲区,然后通过sed编辑文本,在输出到指定的文件,sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用 ...

  5. Python学习笔记3

    __slots__ 如果我们想要限制class的属性怎么办?比如,只允许对Student实例添加name和age属性. 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__s ...

  6. [2013-07-22]varnish-cache 安装配置及体验笔记

    varnish安装 ubuntu12安装参考 其他系统参考 如果选择了直接从源安装的方式的话,就不要自己去编译了,以免出现意外(悲剧的我,varnishlog 有点问题,之前先编译安装了,再从源安装, ...

  7. html:table属性cellpadding

    cellpadding:单元格边距(空白区域) colspan:可以横跨的列数(td/th都算一列) 详细:http://www.dreamdu.com/xhtml/attribute_cellpad ...

  8. Wifi长距离传输

    本人从事Linux驱动开发.现在要实现两端之间wifi长距离(1km左右)传输视频数据(全向天线)的功能.目前用的平台是Atheros AR9342.我在网上查到一些资料是关于禁用802.11的CSM ...

  9. selenium系列------元素定位套路

    selenium定位分为上三门,平三门,下三门, id,name,linktext上三门, class ,css,js平三门, xpath,tag名,复数定位(定位一组然后选index元素).

  10. C++ operator bool

    雕虫小技: #include <iostream> struct A{ operator bool(){ return false; } }; int main() { A a{}; if ...