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