In three steps:
1. Create a folder named tlds under the WEB-INF, and then create a tld file, such as formatTime. tld, the content is:
Copy codeThe Code is as follows: <? 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>
2. Create a class with the following content:Copy codeThe Code is as follows: 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 timestamp;
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 = timestamp;
}
}
3. Create a jsp page for test:Copy codeThe Code is as follows: <% @ 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> custom tag example </title>
</Head>
<Body>
<P>
Convert "20090403132233"
</P>
Format: dd MMMM yyyy showTH: true style: after Upper is converted to: <tf: formatTimeAsString timestamp = "20090403132233" format = "dd MMMM yyyy" showTH = "true" style = "Upper"/> <br/>
Format: dd MMMM yyyy showTH: true style: After Lower is converted to: <tf: formatTimeAsString timestamp = "20090403132233" format = "dd MMMM yyyy" showTH = "true" style = "Lower"/> <br/>
Format: dd MMMM yyyy showTH: false style: after Upper is converted to: <tf: formatTimeAsString timestamp = "20090403132233" format = "dd MMMM yyyy" showTH = "false" style = "Upper"/> <br/>
Format: dd MMMM yyyy showTH: true style: "" converted to: <tf: formatTimeAsString timestamp = "20090403132233" format = "dd MMMM yyyy" showTH = "true" style = ""/> <br/>
</Body>
</Html>
OK. Here, timestamp is a given value. If it is dynamically obtained through the $ {} tag, you needCopy codeThe Code is as follows: <attribute>
<Name> timestamp </name>
</Attribute>
ChangeCopy codeThe Code is as follows: <attribute>
<Name> timestamp </name>
<Rtexprvalue> true </rtexprvalue>
</Attribute>