KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > values > types > DateType


1 /*
2  * Copyright (c) 2004, Rob Gordon.
3  */

4 package org.oddjob.values.types;
5
6 import java.text.ParseException JavaDoc;
7 import java.util.Date JavaDoc;
8
9 import org.oddjob.util.DateHelper;
10
11 /**
12  * @oddjob.description Allows a date to be specified in different formats.
13  * <p>
14  * Dates can also be specified directly as a {@link ValueType}
15  * without the need for this type but
16  * must be a fixed format currently fixed at dd-MMM-yyyy.
17  * <p>
18  * For more information on format see
19  * <a HREF="http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html">
20  * http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html</a>.
21  *
22  * @oddjob.example
23  *
24  *<pre>
25  * &lt;sequential&gt;
26  * &lt;variables id="vars"&gt;
27  * &lt;date name="xmas" format="dd/MM/yy" date="25/12/05"/&gt;
28  * &lt;/variables&gt;
29  * &lt;echo text="Christmas will start at ${vars.xmas}"/&gt;
30  * &lt;/sequential&gt;
31  *</pre>
32  *
33  * @author Rob Gordon.
34  */

35 public class DateType {
36
37     /**
38      * @oddjob.property
39      * @oddjob.description A date in text in the format specified by format.
40      * @oddjob.required Yes.
41      */

42     private String JavaDoc date;
43     
44     /**
45      * @oddjob.property
46      * @oddjob.description The time zone the date is for.
47      * @oddjob.required No.
48      */

49     private String JavaDoc timeZone;
50
51     /**
52      * Resolve this type.
53      *
54      * @param required The desired result class.
55      * @return A date or null.
56      * @throws ParseException If the date string can't be converted to a date.
57      */

58     public Date JavaDoc valueFor(Class JavaDoc required)
59     throws ParseException JavaDoc {
60         if (date == null) {
61             return null;
62         }
63         return DateHelper.parseDateTime(date);
64     }
65     
66     
67     public void setDate(String JavaDoc date) {
68         this.date = date;
69     }
70     
71     public String JavaDoc getDate() {
72         return date;
73     }
74     
75     public void setTimeZone(String JavaDoc timeZoneId) {
76         this.timeZone = timeZoneId;
77     }
78     
79     public String JavaDoc getTimeZone() {
80         return timeZone;
81     }
82     
83     public String JavaDoc toString() {
84         try {
85             return valueFor(Object JavaDoc.class).toString();
86         } catch (ParseException JavaDoc e) {
87             return e.getMessage();
88         }
89     }
90 }
91
Popular Tags