1   package com.acme;
2   
3   import java.text.SimpleDateFormat;
4   import java.util.Date;
5   import java.util.TimeZone;
6   
7   import javax.servlet.jsp.JspException;
8   import javax.servlet.jsp.JspTagException;
9   import javax.servlet.jsp.PageContext;
10  import javax.servlet.jsp.tagext.BodyContent;
11  import javax.servlet.jsp.tagext.BodyTagSupport;
12  import javax.servlet.jsp.tagext.Tag;
13  
14  public class DateTag extends BodyTagSupport
15  {
16      Tag parent;
17      BodyContent body;
18      String tz="GMT";
19  
20      public void setParent(Tag parent) {this.parent=parent;}
21      public Tag getParent() {return parent;}
22      public void setBodyContent(BodyContent content) {body=content;}
23      public void setPageContext(PageContext pageContext) {}
24  
25      public void setTz(String value) {tz=value;}
26  
27      public int doStartTag() throws JspException {return EVAL_BODY_TAG;}
28      
29      public int doEndTag() throws JspException {return EVAL_PAGE;}
30  
31      public void doInitBody() throws JspException {}
32  
33      public int doAfterBody() throws JspException {
34  	try
35  	{
36              SimpleDateFormat format = new SimpleDateFormat(body.getString());
37              format.setTimeZone(TimeZone.getTimeZone(tz));
38  	    body.getEnclosingWriter().write(format.format(new Date()));
39  	    return SKIP_BODY;
40  	}
41  	catch (Exception ex) {
42              ex.printStackTrace();
43              throw new JspTagException(ex.toString());
44  	}
45      }
46  
47      public void release()
48      {
49  	body=null;
50      }
51  }
52