KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > config > types > DateType


1 package org.sapia.soto.config.types;
2
3 import java.text.DateFormat JavaDoc;
4 import java.text.ParseException JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6
7 import org.sapia.util.xml.confix.ConfigurationException;
8 import org.sapia.util.xml.confix.ObjectCreationCallback;
9
10 /**
11  * @author Yanick Duchesne
12  *
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class DateType implements ObjectCreationCallback{
20     
21     private String JavaDoc _value, _pattern;
22     
23     public void setValue(String JavaDoc value){
24         _value = value;
25     }
26     
27     /**
28      * A date parsing pattern, as specified in the JDK's <code>SimpleDateFormat</code>
29      * API doc.
30      *
31      * @param pattern the pattern that should be used to parse the value
32      * held within this instance.
33      *
34      * @see SimpleDateFormat
35      */

36     public void setPattern(String JavaDoc pattern){
37         _pattern = pattern;
38     }
39     
40     /**
41    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
42    */

43   public Object JavaDoc onCreate() throws ConfigurationException {
44     if(_value == null){
45         throw new ConfigurationException("Date value not specified");
46     }
47     try{
48             if(_pattern == null){
49                 return DateFormat.getDateInstance().parse(_value);
50             }
51             else{
52                 SimpleDateFormat JavaDoc fm = new SimpleDateFormat JavaDoc(_pattern);
53                 return fm.parseObject(_value);
54             }
55     }catch(ParseException JavaDoc e){
56         throw new ConfigurationException("Could not parse date: " + _value + "; " +
57         "have you specified an appropriate pattern?", e);
58     }
59   }
60 }
61
Popular Tags