01: /*
02: * The contents of this file are subject to the Sapient Public License
03: * Version 1.0 (the "License"); you may not use this file except in compliance
04: * with the License. You may obtain a copy of the License at
05: * http://carbon.sf.net/License.html.
06: *
07: * Software distributed under the License is distributed on an "AS IS" basis,
08: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
09: * 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: import java.text.DateFormat;
21: import java.text.ParseException;
22: import java.text.SimpleDateFormat;
23: import java.util.Date;
24:
25: import org.sape.carbon.core.config.type.ConfigurationTypeHandler;
26: import org.sape.carbon.core.config.type.TypeConversionException;
27:
28: /**
29: * <P>Parses and formats between Strings and Dates in the format of
30: * "MM/dd/yyyy". An example is "08/07/2002" or August 7th, 2002.</P>
31: *
32: * Copyright 2002 Sapient
33: * @since carbon 1.0
34: * @author Greg Hinkle, January 2002
35: * @version $Revision: 1.10 $($Author: dvoet $ / $Date: 2003/05/05 21:21:20 $)
36: */
37: public class DateTypeHandler implements ConfigurationTypeHandler {
38:
39: /** A Date Formatter for the format of "MM/dd/YYYY" */
40: private static DateFormat dateFormat;
41:
42: static {
43: DateTypeHandler.dateFormat = new SimpleDateFormat(
44: "MM/dd/yyyy H:m:s");
45: }
46:
47: /**
48: * <P>Parses a string into a Date object.</P>
49: *
50: * @param stringValue the String representation of the requested Date
51: * @param type String
52: * @return Date version of the given String
53: * @throws TypeConversionException when the string does not represent
54: * a valid Date
55: */
56: public Object toObject(Class type, String stringValue)
57: throws TypeConversionException {
58:
59: Date object = null;
60: try {
61: object = DateTypeHandler.dateFormat.parse(stringValue);
62: } catch (ParseException pe) {
63: throw new TypeConversionException(this .getClass(),
64: "Couldn't parse date", pe);
65: }
66: return object;
67: }
68:
69: /**
70: * <P>Formats an Date into a String value.</P>
71: *
72: * @param objectValue the Date to be formatted
73: * @return String version of the Date
74: * @throws ClassCastException when past an object other than a Date
75: */
76: public String toString(Object objectValue) {
77: return DateTypeHandler.dateFormat.format((Date) objectValue);
78: }
79:
80: }
|