一、有WEB-INF下建立一个datetag.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<tag>
<name>date</name>
<tag-class>com.hoolai.pirates.football.util.DateTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
二、新建一个类DateTag
package com.hoolai.pirates.football.util;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class DateTag extends TagSupport {
private static final long serialVersionUID = -2312310581852395045L;
private String value;
@Override
public int doStartTag() throws JspException {
String vv = "" + value;
long time = Long.valueOf(vv);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = dateformat.format(c.getTime());
try {
pageContext.getOut().write(s);
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
public void setValue(String value) {
this.value = value;
}
}
三、在jsp中引入
<%@ taglib prefix="lzq" uri="/WEB-INF/lib/tld/datetag.tld"%>
四、页面中用到的地方
<td align="left"><input name="endTime" type="text" value="<lzq:date value="${res.endTime}"/>" class="input_bg" /></td>
这样在servlet里传过来的是long,在页面上用jstl可以格式化为 2014-05-04 18:18:22