KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > editor > DateTimeEditor


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.editor;
8
9 import java.text.DateFormat JavaDoc;
10 import java.text.ParseException JavaDoc;
11 import java.text.SimpleDateFormat JavaDoc;
12 import java.util.Date JavaDoc;
13
14 /**
15  * Description of the Class
16  *
17  * @author Laurent Etiemble
18  * @version $Revision: 1.5 $
19  * @todo Javadoc to complete
20  */

21 public class DateTimeEditor extends GenericEditor
22 {
23    /** Description of the Field */
24    protected DateFormat JavaDoc fmts[];
25
26
27    /** Constructor for the DateEditor object */
28    public DateTimeEditor()
29    {
30       fmts = new DateFormat JavaDoc[5];
31       fmts[0] = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss z");
32       fmts[1] = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
33       fmts[2] = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm");
34       fmts[3] = new SimpleDateFormat JavaDoc("yyMMdd HH:mm:ss");
35       fmts[4] = new SimpleDateFormat JavaDoc("yyMMdd HH:mm");
36    }
37
38
39    /**
40     * Gets the asText attribute of the DateEditor object
41     *
42     * @return The asText value
43     */

44    public String JavaDoc getAsText()
45    {
46       if (this.value != null)
47       {
48          return this.fmts[0].format((Date JavaDoc) this.value);
49       }
50       else
51       {
52          return "";
53       }
54    }
55
56
57    /**
58     * Sets the asText attribute of the DateEditor object
59     *
60     * @param s The new asText value
61     */

62    public void setAsText(String JavaDoc s)
63    {
64       if (s.equals(""))
65       {
66          this.value = null;
67          return;
68       }
69       for (int i = 0; i < fmts.length; i++)
70       {
71          try
72          {
73             this.value = fmts[i].parse(s);
74             this.firePropertyChange();
75             return;
76          }
77          catch (ParseException JavaDoc pe)
78          {
79             // Do nothing
80
}
81       }
82    }
83 }
84
85
Popular Tags