1 package prefuse.data.parser; 2 3 import java.sql.Time ; 4 import java.text.DateFormat ; 5 6 17 public class TimeParser extends DateParser { 18 19 22 public TimeParser() { 23 this(DateFormat.getTimeInstance(DateFormat.SHORT)); 24 } 25 26 30 public TimeParser(DateFormat dateFormat) { 31 super(dateFormat); 32 } 33 34 38 public Class getType() { 39 return Time .class; 40 } 41 42 45 public boolean canParse(String val) { 46 try { 47 parseTime(val); 48 return true; 49 } catch ( DataParseException e ) { 50 return false; 51 } 52 } 53 54 57 public Object parse(String val) throws DataParseException { 58 return parseTime(val); 59 } 60 61 67 public Time parseTime(String text) throws DataParseException { 68 m_pos.setErrorIndex(0); 69 m_pos.setIndex(0); 70 71 Time t = null; 73 try { 74 t = Time.valueOf(text); 75 m_pos.setIndex(text.length()); 76 } catch ( IllegalArgumentException e ) { 77 t = null; 78 } 79 if ( t == null ) { 80 java.util.Date d1 = m_dfmt.parse(text, m_pos); 81 if ( d1 != null ) { 82 t = new Time (d1.getTime()); 83 } 84 } 85 86 if ( t == null || m_pos.getIndex() < text.length() ) { 89 throw new DataParseException("Could not parse Date: "+text); 90 } else { 91 return t; 92 } 93 } 94 95 } | Popular Tags |