jstl (struts bean、fmt等标签) - 格式化输出日期和日期型字符串

方法1:jstl

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

<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib uri="
http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="
http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
     <head>
       <title>My JSP 'fmt.jsp' starting page</title>
     </head>
  
     <body>
      <c:set var="salary" value="3540.2301"/>
      <c:set var="total" value="56225.2301"/>
      <fmt:setLocale value="en_US"/>
      currency:<fmt:formatNumber value="${salary}" type="currency" currencyCode="USD"/><br>
      percent:<fmt:formatNumber value="${salary/total}" type="percent" maxFractionDigits="4"/><br>
      <hr>
      <jsp:useBean id="now" class="java.util.Date"></jsp:useBean>
      <fmt:setLocale value="zh_CN"/>
      full--><fmt:formatDate value="${now}" type="both" dateStyle="full" timeStyle="full"/><br>
      long--><fmt:formatDate value="${now}" type="both" dateStyle="long" timeStyle="long"/><br>
      medium--><fmt:formatDate value="${now}" type="both" dateStyle="medium" timeStyle="medium"/><br>
      default--><fmt:formatDate value="${now}" type="both" dateStyle="default" timeStyle="default"/><br>
      short--><fmt:formatDate value="${now}" type="both" dateStyle="short" timeStyle="short"/><br>
     </body>
</html>

今天是:<fmt:formatDate value="${now}" pattern="G yyyy年MM月dd日 E"/><br>
现在是:<fmt:formatDate value="${now}" pattern="a HH:mm:ss.S z"/>
结果:
今天是:公元 2007年10月19日 星期五
现在是:下午 20:04:11.484 CST

<%@ 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.
<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.
<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.
<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/>

<fmt:formatDate value="${这里可以放日期和字符串}" pattern="yyyy年MM月dd日"/>

 

方法2:用Struts标签格式化输出

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>

格式化时间如:2008-06-23 11:00:22
<bean:write name="msg" property="messageDate"   format= "yyyy-MM-dd HH:mm:ss" />

格式化时间如:2008-06-23 11:00
<bean:write name="msg" property="messageDate"   format= "yyyy-MM-dd HH:mm" />

格式化时间如:06-23
<bean:write   name= "sdrecord "   property= "sdTime "   format= "MM-dd"/>

补充格式化数字:

数字,不足4位前面自动补0 如:04 输入0004
<bean:write   name= "sdrecord "   property= "sdTime "   format= "0000 "/>

 

附Struts bean:write

首先要想使用struts的标签库,则必须在jsp中指定所使用的标签库地uri,即标签库的标准。引入方法如下:

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>

接下来就可使用该标签库中的标签了,前缀是bean。我们这篇文章主要介绍write标签,bean:write相 当于<%=request.getAttribute("something")%> 其中something是属性的名字。下面bean:write的详细介绍如下:

bean:write常用的属性有如下几个:

1。name,用来指定属性的名字

2。filter,用来指定是否屏蔽到属性值的HTML格式

3。property,用来指定name所代表的对象的属性名字

4。format,用来指定显示的时间,数字,日期等的格式

例子一:

某处设置了request.setAttribute("hello","hello world");

则在某个jsp页面中,用struts的write标签取出并显示的方式如下:

<bean:write name="hello"/>,则页面上显示出hello world。

例子二:

某处设置了request.setAttribute("bj","<font color='red>欢迎你</font>");

则在某个jsp页面中,用struts的write标签取出并按红色的方式显示的方式如下:

<bean:write name="bj" filter="false"/>,则页面上显示出红色的欢迎你。如果filter属性不设置为false,则默认为true,那么显示出的内容就为<font color='red>欢迎你</font>。

例子三:

某处设置了request.setAttribute("date",new Date());

则在某个jsp页面中,用struts的write标签取出并按指定方式显示日期的方法如下:

<bean:write name=“date”/>,此为默认的显示方法,显示出的时间为:Fri Mar 28 15:04:21 CST 2008

<bean:write name=“date” format="yyyy-MM-dd HH:mm:ss"/>,此为自己指定日期的显示格式,显示出的时间为2008-3-28 15:04:21

例子四:

某处设置了request.setAttribute("n",“1223333.333”);

则在某个jsp页面中,用struts的write标签取出并按指定方式显示数字的方法如下:

<bean:write name=“n”/>,此为默认的显示方法,显示出的数字位1223333.333

<bean:write name=“n” format="###,###.####"/>,此为自己指定数字的显示格式,显示出的时间为1,223,333.333

如果希望小数点后的不足四位时,缺位补0,则应

<bean:write name=“n” format="###,###.0000"/>,此为自己指定数字的显示格式,显示出的时间为1,223,333.3330

例子五:

假如有User类和Groupe类,User类有属性名字userName,年龄age,性别sex和所属的Groupe,Groupe类有属性组名groupeName,并均具有相应的get和set方法。

某处设置了request.setAttribute("user",new User("张三","20","男",new Groupe("三组")));

则在某个jsp页面中,用struts的write标签取出并按指定方式显示结构体的方法如下:

用户名:<input type="text" value="<bean:write name=“user” property="userName"/>">

年龄:<input type="text" value="<bean:write name=“user” property="age"/>">

性别:<input type="text" value="<bean:write name=“user” property="sex"/>">

组名:<input type="text" value="<bean:write name=“user” property="groupe.groupeName"/>">

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值