Java毫秒數時間格式化插件

datetag.tld放在WEB-INF目錄下。

1.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.crm.tag.date.DateTag</tag-class>       
        <body-content>JSP</body-content >
        <attribute>
            <name>value</name >
            <required>true</required >
            <rtexprvalue>true</rtexprvalue >
        </attribute>
        <attribute>
        	<name>format</name>
        </attribute>
    </tag>
</taglib>

2.web.xml

<jsp-config>
<taglib>
<taglib-uri>/date-tag</taglib-uri>
<taglib-location>/WEB-INF/datetag.tld</taglib-location>
</taglib>
</jsp-config>
</pre><pre name="code" class="html">3.DateTag.java
public class DateTag extends TagSupport{
        /**
	 * 
	 */
	private static final long serialVersionUID = -991134678872622289L;
	
	private String value;
	
	private String format;
	
	@Override
	public int doStartTag() throws JspException {
		String vv = ""+value;
		String formatf = "yyyy-MM-dd HH:mm:ss";
		if(format!=null && !format.equals("")){
			formatf = format;
		}
		long time = Long.valueOf(vv);
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		SimpleDateFormat sdf = new SimpleDateFormat(formatf);
		String s = sdf.format(c.getTime());
		try {
            pageContext.getOut().write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
		return super.doStartTag();
	}
	public void setValue(String value){
		this.value = value;
	}
	public void setFormat(String format){
		this.format = format;
	}
}

4.調用方法


<%@taglib uri="/date-tag" prefix="date" %>
<date:date value="${createTime}" />




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章