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