1 /*
2 * Copyright (C) The Spice Group. All rights reserved.
3 *
4 * This software is published under the terms of the Spice
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.codehaus.spice.converter.lib;
9
10 import java.text.DateFormat;
11 import java.util.Date;
12 import org.codehaus.spice.converter.AbstractConverter;
13 import org.codehaus.spice.converter.ConverterException;
14
15 /***
16 * String to Date converter.
17 *
18 * <p>Parses a date according to the same rules as the Date.parse() method. In
19 * particular it recognizes the IETF standard date syntax:</p>
20 * <p>"Sat, 12 Aug 1995 13:30:00 GMT"</p>
21 *
22 * @author Peter Donald
23 * @see java.util.Date#parse
24 * @version $Revision: 1.1 $ $Date: 2003/12/02 08:37:56 $
25 */
26 public class StringToDateConverter
27 extends AbstractConverter
28 {
29 /***
30 * Construct the converter.
31 */
32 public StringToDateConverter()
33 {
34 this( null );
35 }
36
37 /***
38 * Construct the converter with a default value.
39 * If the default value is non-null, it will be returned if unable
40 * to convert object to correct type.
41 *
42 * @param defaultValue the default value
43 */
44 public StringToDateConverter( final Date defaultValue )
45 {
46 super( String.class, Date.class, defaultValue );
47 }
48
49 /***
50 * Converts a String to a Date.
51 *
52 * @param object the original object to convert
53 * @param context the context in which to convert object (unused)
54 * @return the converted object
55 * @throws ConverterException if error converting object
56 */
57 public Object convert( final Object object, final Object context )
58 throws ConverterException
59 {
60 try
61 {
62 final DateFormat formatter = DateFormat.getInstance();
63 return formatter.format( object.toString() );
64 }
65 catch( final NumberFormatException nfe )
66 {
67 return noConvert( object, nfe );
68 }
69 }
70 }
71
This page was automatically generated by Maven