分三步:
一、在WEB-INF 下建一個(gè)名為tlds的文件夾,再建一個(gè)tld文件,如 formatTime.tld,內(nèi)容為:
復(fù)制代碼 代碼如下:
?xml version="1.0" encoding="ISO-8859-1" ?>
!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
taglib>
tlibversion>1.0/tlibversion>
jspversion>1.1/jspversion>
shortname>ntuc/shortname>
tag>
name>formatTimeAsString/name>
tagclass>com.ufinity.taglibTest.FormatTimeTag/tagclass>
bodycontent>empty/bodycontent>
attribute>
name>timestamp/name>
/attribute>
attribute>
name>format/name>
/attribute>
attribute>
name>showTH/name>
/attribute>
attribute>
name>style/name>
/attribute>
/tag>
/taglib>
二、建一個(gè)class,內(nèi)容為:
復(fù)制代碼 代碼如下:
package com.ufinity.taglibTest;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.servlet.jsp.tagext.TagSupport;
/**
* Description of the class
*
* @author Wangqy
* @version 1.0
* @since 2009-8-25
*/
public class FormatTimeTag extends TagSupport {
/**
* serialVersionUID long
*/
private static final long serialVersionUID = 8757501937718830491L;
private String timest
private String format;
private String showTH;
private String style;
public int doEndTag()
{
try
{
String info = this.convertDateTime(timestamp, format, Boolean.parseBoolean(showTH), style);
pageContext.getOut().println(info);
}
catch (IOException e) {
}
return EVAL_PAGE;
}
private String convertDateTime(String dateTime, String formater, boolean showTH, String caseStyle) {
String timePosted = null;
SimpleDateFormat dateFm = null;
DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
Date formatTime = null;
try {
formatTime = format.parse(dateTime);
} catch (ParseException e) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatTime);
if (showTH) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
String daySuffix = "th";
if ((day % 10) == 1) {
daySuffix = ((day / 10) == 1) ? "th" : "st";
} else if ((day % 10) == 2) {
daySuffix = (day == 12) ? "th" : "nd";
} else if ((day % 10) == 3) {
daySuffix = (day == 13) ? "th" : "rd";
}
formater = formater.substring(0, formater.indexOf(" ")) + "'"
+ daySuffix + "'"
+ formater.substring(formater.indexOf(" "));
dateFm = new SimpleDateFormat(formater, Locale.ENGLISH);
} else {
dateFm = new SimpleDateFormat(formater, Locale.ENGLISH);
}
timePosted = dateFm.format(formatTime);
if (caseStyle.equals("Upper")) {
timePosted = timePosted.toUpperCase();
} else if (caseStyle.equals("Lower")) {
timePosted = timePosted.toLowerCase();
}
return timePosted;
}
public void setFormat(String format) {
this.format = format;
}
public void setShowTH(String showTH) {
this.showTH = showTH;
}
public void setStyle(String style) {
this.style = style;
}
public void setTimestamp(String timestamp) {
this.timestamp = timest
}
}
三、建個(gè)jsp頁面測(cè)試下:
復(fù)制代碼 代碼如下:
%@ page language="java" pageEncoding="utf-8"%>
%@ taglib uri="WEB-INF/tlds/formatTime.tld" prefix="tf" %>
html>
head>
meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
title>自定義標(biāo)簽示例/title>
/head>
body>
p>
轉(zhuǎn)換“20090403132233”
/p>
format:dd MMMM yyyy showTH:true style:Upper 轉(zhuǎn)換后為:tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style="Upper"/>br/>
format:dd MMMM yyyy showTH:true style:Lower 轉(zhuǎn)換后為:tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style="Lower"/>br/>
format:dd MMMM yyyy showTH:false style:Upper 轉(zhuǎn)換后為:tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="false" style="Upper"/>br/>
format:dd MMMM yyyy showTH:true style:"" 轉(zhuǎn)換后為:tf:formatTimeAsString timestamp="20090403132233" format="dd MMMM yyyy" showTH="true" style=""/>br/>
/body>
/html>
ok了。這里timestamp是一個(gè)給定值,如果是通過${}標(biāo)簽動(dòng)態(tài)取的話,需要將tld文件的
復(fù)制代碼 代碼如下:
attribute>
name>timestamp/name>
/attribute>
修改為
復(fù)制代碼 代碼如下:
attribute>
name>timestamp/name>
rtexprvalue>true/rtexprvalue>
/attribute>
您可能感興趣的文章:- JSP 自定義標(biāo)簽
- JSP 自定義標(biāo)簽之一 簡(jiǎn)單實(shí)例
- JSP自定義標(biāo)簽Taglib實(shí)現(xiàn)過程重點(diǎn)總結(jié)
- 基于JSP 自定義標(biāo)簽使用實(shí)例介紹
- jsp自定義標(biāo)簽技術(shù)(實(shí)現(xiàn)原理與代碼以及平臺(tái)搭建步驟)
- JSP自定義標(biāo)簽獲取用戶IP地址的方法
- jsp簡(jiǎn)單自定義標(biāo)簽的forEach遍歷及轉(zhuǎn)義字符示例
- JSP自定義標(biāo)簽rtexprvalue屬性用法實(shí)例分析
- JSP自定義標(biāo)簽案例分析