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