KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > type > handlers > DateTypeHandler


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.config.type.handlers;
19
20
21 import java.text.DateFormat JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25
26 import org.sape.carbon.core.config.type.ConfigurationTypeHandler;
27 import org.sape.carbon.core.config.type.TypeConversionException;
28
29 /**
30  * <P>Parses and formats between Strings and Dates in the format of
31  * "MM/dd/yyyy". An example is "08/07/2002" or August 7th, 2002.</P>
32  *
33  * Copyright 2002 Sapient
34  * @since carbon 1.0
35  * @author Greg Hinkle, January 2002
36  * @version $Revision: 1.10 $($Author: dvoet $ / $Date: 2003/05/05 21:21:20 $)
37  */

38 public class DateTypeHandler implements ConfigurationTypeHandler {
39
40
41     /** A Date Formatter for the format of "MM/dd/YYYY" */
42     private static DateFormat JavaDoc dateFormat;
43
44     static {
45         DateTypeHandler.dateFormat = new SimpleDateFormat JavaDoc("MM/dd/yyyy H:m:s");
46     }
47
48     /**
49      * <P>Parses a string into a Date object.</P>
50      *
51      * @param stringValue the String representation of the requested Date
52      * @param type String
53      * @return Date version of the given String
54      * @throws TypeConversionException when the string does not represent
55      * a valid Date
56      */

57     public Object JavaDoc toObject(Class JavaDoc type, String JavaDoc stringValue)
58         throws TypeConversionException {
59
60         Date JavaDoc object = null;
61         try {
62             object = DateTypeHandler.dateFormat.parse(stringValue);
63         } catch (ParseException JavaDoc pe) {
64             throw new TypeConversionException(
65                 this.getClass(),
66                     "Couldn't parse date", pe);
67         }
68         return object;
69     }
70
71     /**
72      * <P>Formats an Date into a String value.</P>
73      *
74      * @param objectValue the Date to be formatted
75      * @return String version of the Date
76      * @throws ClassCastException when past an object other than a Date
77      */

78     public String JavaDoc toString(Object JavaDoc objectValue) {
79         return DateTypeHandler.dateFormat.format((Date JavaDoc) objectValue);
80     }
81
82 }
83
Popular Tags