Code example for ParsePosition

Methods: getIndex, setErrorIndex, setIndex

0
     * @param text the text string to parse 
     * @return the parsed Date value 
     * @throws DataParseException if an error occurs during parsing 
     */ 
    public Date parseDate(String text) throws DataParseException {
        m_pos.setErrorIndex(0);
        m_pos.setIndex(0);
         
        // parse the data value, convert to the wrapper type 
        Date d = null;
        try { 
            d = Date.valueOf(text);
            m_pos.setIndex(text.length());
        } catch ( IllegalArgumentException e ) {
            d = null;
        } 
        if ( d == null ) {
            java.util.Date d1 = m_dfmt.parse(text, m_pos);
            if ( d1 != null ) {
                d = new Date(d1.getTime());
            } 
        } 
         
        // date format will parse substrings successfully, so we need 
        // to check the position to make sure the whole value was used 
        if ( d == null || m_pos.getIndex() < text.length() ) {
            throw new DataParseException("Could not parse Date: "+text);
        } else { 
            return d;
        } 
    }