KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > parser > DateParser


1 package prefuse.data.parser;
2
3 import java.sql.Date JavaDoc;
4 import java.text.DateFormat JavaDoc;
5 import java.text.ParsePosition JavaDoc;
6
7 /**
8  * DataParser instance that parses Date values as java.sql.Time instances,
9  * representing a particular date (but not a specific time on that day).
10  * This class uses a backing {@link java.text.DateFormat} instance to
11  * perform parsing. The DateFormat instance to use can be passed in to the
12  * constructor, or by default the DateFormat returned by
13  * {@link java.text.DateFormat#getDateInstance(int)} with an
14  * argument of {@link java.text.DateFormat#SHORT} is used.
15  *
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class DateParser implements DataParser {
19     
20     protected DateFormat JavaDoc m_dfmt;
21     protected ParsePosition JavaDoc m_pos;
22     
23     /**
24      * Create a new DateParser.
25      */

26     public DateParser() {
27         this(DateFormat.getDateInstance(DateFormat.SHORT));
28     }
29     
30     /**
31      * Create a new DateParser.
32      * @param dateFormat the DateFormat instance to use for parsing
33      */

34     public DateParser(DateFormat JavaDoc dateFormat) {
35         m_dfmt = dateFormat;
36         m_pos = new ParsePosition JavaDoc(0);
37     }
38     
39     /**
40      * Returns java.sql.Date.
41      * @see prefuse.data.parser.DataParser#getType()
42      */

43     public Class JavaDoc getType() {
44         return Date JavaDoc.class;
45     }
46     
47     /**
48      * @see prefuse.data.parser.DataParser#format(java.lang.Object)
49      */

50     public String JavaDoc format(Object JavaDoc value) {
51         return value==null ? null : m_dfmt.format(value);
52     }
53     
54     /**
55      * @see prefuse.data.parser.DataParser#canParse(java.lang.String)
56      */

57     public boolean canParse(String JavaDoc text) {
58         try {
59             parseDate(text);
60             return true;
61         } catch ( DataParseException e ) {
62             return false;
63         }
64     }
65     
66     /**
67      * @see prefuse.data.parser.DataParser#parse(java.lang.String)
68      */

69     public Object JavaDoc parse(String JavaDoc text) throws DataParseException {
70         return parseDate(text);
71     }
72     
73     /**
74      * Parse a Date value from a text string.
75      * @param text the text string to parse
76      * @return the parsed Date value
77      * @throws DataParseException if an error occurs during parsing
78      */

79     public Date JavaDoc parseDate(String JavaDoc text) throws DataParseException {
80         m_pos.setErrorIndex(0);
81         m_pos.setIndex(0);
82         
83         // parse the data value, convert to the wrapper type
84
Date JavaDoc d = null;
85         try {
86             d = Date.valueOf(text);
87             m_pos.setIndex(text.length());
88         } catch ( IllegalArgumentException JavaDoc e ) {
89             d = null;
90         }
91         if ( d == null ) {
92             java.util.Date JavaDoc d1 = m_dfmt.parse(text, m_pos);
93             if ( d1 != null ) {
94                 d = new Date JavaDoc(d1.getTime());
95             }
96         }
97         
98         // date format will parse substrings successfully, so we need
99
// to check the position to make sure the whole value was used
100
if ( d == null || m_pos.getIndex() < text.length() ) {
101             throw new DataParseException("Could not parse Date: "+text);
102         } else {
103             return d;
104         }
105     }
106     
107 } // end of class DateParser
108
Popular Tags