今天在在做项目时发现页面上从数据库中调用日期时,时间也一起显示出来了,比如说我只想要显示日期2008-10-6,但通过语句调用出来时显示是2008-10-6 20:36:42 。
- <logic:present name="all" scope="request">
- <table width="95%" height="500">
- <logic:iterate id="data" name="all" scope="request">
- <tr>
- <td Class="TdClass" height="25" align="left" valign="top">
- ·
- <a href="show.do?did=${data.did}&status=datashow" target="_blank">${data.dtitle}</a>
- </td>
- <td Class="TdClass3" height="25" align="right" width="18%" valign="top"> ${data.datatime}</td><!--这是我调用时间的语句-->
- </tr>
- </logic:iterate>
- </table>
- </logic:present>
以上是我jsp页面调用时间的例子。我后来上网查了以下,总结有以下三种方式能够得到:
第一种:通过jstl标签:
- <%@ 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
- 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"/>
来源:http://sunxboy.javaeye.com/blog/168764
第二种方法:用struts中的bean标签:
在我的程序中加入以下代码:<bean:write name="form" property="datetime" format="yyyy-MM-dd"/>
加入以后的代码是:
- <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
- <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
- <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
- <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
- <logic:present name="all" scope="request">
- <table width="95%" height="500">
- <logic:iterate id="data" name="all" scope="request">
- <tr>
- <td Class="TdClass" height="25" align="left" valign="top">
- ·
- <a href="show.do?did=${data.did}&status=datashow" target="_blank">${data.dtitle}</a>
- </td>
- <td Class="TdClass3" height="25" align="right" width="18%" valign="top">
- <bean:write name="data" property="datatime" format="yyyy-MM-dd"/>
- </td><!--这是我调用时间的语句-->
- </tr>
- </logic:iterate>
- </table>
第三种方法:用java脚本实现:
如果要用JAVA脚本的话,
- <%
- SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
- String str = s.format((Date)request.getAttribute("date"));
- out.println(str);
- %>