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 import javax.servlet.jsp.JspException;
25 import javax.servlet.jsp.JspTagException;
26 import javax.servlet.jsp.PageContext;
27 import javax.servlet.jsp.tagext.BodyContent;
28 import javax.servlet.jsp.tagext.BodyTagSupport;
29 import javax.servlet.jsp.tagext.Tag;
30
31 @SuppressWarnings("serial")
32 public class DateTag extends BodyTagSupport
33 {
34 Tag parent;
35 BodyContent body;
36 String tz="GMT";
37
38 public void setParent(Tag parent) {this.parent=parent;}
39 public Tag getParent() {return parent;}
40 public void setBodyContent(BodyContent content) {body=content;}
41 public void setPageContext(PageContext pageContext) {}
42
43 public void setTz(String value) {tz=value;}
44
45 public int doStartTag() throws JspException {return EVAL_BODY_BUFFERED;}
46
47 public int doEndTag() throws JspException {return EVAL_PAGE;}
48
49 public void doInitBody() throws JspException {}
50
51 public int doAfterBody() throws JspException {
52 try
53 {
54 SimpleDateFormat format = new SimpleDateFormat(body.getString());
55 format.setTimeZone(TimeZone.getTimeZone(tz));
56 body.getEnclosingWriter().write(format.format(new Date()));
57 return SKIP_BODY;
58 }
59 catch (Exception ex) {
60 ex.printStackTrace();
61 throw new JspTagException(ex.toString());
62 }
63 }
64
65 public void release()
66 {
67 body=null;
68 }
69 }
70