KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > DatePropertyEditor


1 /*
2  * Copyright 1999, 2000 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.text.ParsePosition JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.sql.Date JavaDoc;
23
24 /**
25  * PropertyEditor implementation for a java.sql.Date property.
26  *
27  * @author Craig R. McClanahan
28  * @revision $Date: 2004/02/27 14:58:56 $ $Revision: 1.2 $
29  */

30 public class DatePropertyEditor extends PropertyEditorSupport JavaDoc {
31
32
33     // ----------------------------------------------------- Instance Variables
34

35
36     /**
37      * The date format to which dates converted by this property editor
38      * must conform.
39      */

40     private SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("MM/dd/yyyy");
41
42
43     // --------------------------------------------------------- Public Methods
44

45
46     /**
47      * Convert our Date object into a String that conforms to our
48      * specified <code>format</code>, and return it. If this is not
49      * possible, return <code>null</code>.
50      */

51     public String JavaDoc getAsText() {
52
53         try {
54             Date JavaDoc date = (Date JavaDoc) getValue();
55             return (format.format(date));
56         } catch (ClassCastException JavaDoc e) {
57             return (null);
58         } catch (IllegalArgumentException JavaDoc e) {
59             return (null);
60         }
61
62     }
63
64
65     /**
66      * Convert the specified String value into a Date, if it conforms to
67      * our specified <code>format</code> , else throw IllegalArgumentException.
68      *
69      * @param value String value to be converted
70      *
71      * @exception IllegalArgumentException if a conversion error occurs
72      */

73     public void setAsText(String JavaDoc value) throws IllegalArgumentException JavaDoc {
74
75         // Validate the format of the input string
76
if (value == null)
77             throw new IllegalArgumentException JavaDoc
78                 ("Cannot convert null String to a Date");
79         if (value.length() != 10)
80             throw new IllegalArgumentException JavaDoc
81                 ("String '" + value + "' has invalid length " +
82                  value.length());
83         for (int i = 0; i < 10; i++) {
84             char ch = value.charAt(i);
85             if ((i == 2) || (i == 5)) {
86                 if (ch != '/')
87                     throw new IllegalArgumentException JavaDoc
88                         ("String '" + value + "' missing slash at index " +
89                          i);
90             } else {
91                 if (!Character.isDigit(ch))
92                     throw new IllegalArgumentException JavaDoc
93                         ("String '" + value + "' missing digit at index " +
94                          i);
95             }
96         }
97
98         // Convert the incoming value to a java.sql.Date
99
java.util.Date JavaDoc temp = format.parse(value, new ParsePosition JavaDoc(0));
100         java.sql.Date JavaDoc date = new java.sql.Date JavaDoc(temp.getTime());
101         setValue(date);
102     }
103
104         
105 }
106
Popular Tags