Code example for ParsePosition

Methods: getErrorIndex, getIndex, setErrorIndex, setIndex

0
     * @see #parse(String, ParsePosition) 
     * @stable ICU 2.0 
     */ 
    public Date parse(String text) throws ParseException
    { 
        ParsePosition pos = new ParsePosition(0);
        Date result = parse(text, pos);
        if (pos.getIndex() == 0) // ICU4J
            throw new ParseException("Unparseable date: \"" + text + "\"" ,
                                     pos.getErrorIndex()); // ICU4J
        return result;
    } 
 
    /** 
     * Parses a date/time string according to the given parse position. 
     * For example, a time text "07/10/96 4:5 PM, PDT" will be parsed 
     * into a Calendar that is equivalent to Date(837039928046). Before 
     * calling this method the caller should initialize the calendar 
     * in one of two ways (unless existing field information is to be kept): 
     * (1) clear the calendar, or (2) set the calendar to the current date 
     * (or to any date whose fields should be used to supply values that 
     * are missing in the parsed date). For example, Chinese calendar dates 
     * do not normally provide an era/cycle; in this case the calendar that 
     * is passed in should be set to a date within the era that should be 
     * assumed, normally the current era. 
     * 
     * <p> By default, parsing is lenient: If the input is not in the form used 
     * by this object's format method but can still be parsed as a date, then 
     * the parse succeeds.  Clients may insist on strict adherence to the 
     * format by calling setLenient(false). 
     * 
     * @see #setLenient(boolean) 
     * 
     * @param text  The date/time string to be parsed 
     * 
     * @param cal   The calendar set on input to the date and time to be used 
     *              for missing values in the date/time string being parsed, 
     *              and set on output to the parsed date/time. In general, this 
     *              should be initialized before calling this method - either 
     *              cleared or set to the current date, depending on desired 
     *              behavior. If this parse fails, the calendar may still 
     *              have been modified. When the calendar type is different 
     *              from the internal calendar held by this DateFormat 
     *              instance, calendar field values will be parsed based 
     *              on the internal calendar initialized with the time and 
     *              the time zone taken from this calendar, then the 
     *              parse result (time in milliseconds and time zone) will 
     *              be set back to this calendar. 
     * 
     * @param pos   On input, the position at which to start parsing; on 
     *              output, the position at which parsing terminated, or the 
     *              start position if the parse failed. 
     * @stable ICU 2.0 
     */ 
    public abstract void parse(String text, Calendar cal, ParsePosition pos);
 
    /** 
     * Parses a date/time string according to the given parse position.  For 
     * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date 
     * that is equivalent to Date(837039928046). 
     * 
     * <p> By default, parsing is lenient: If the input is not in the form used 
     * by this object's format method but can still be parsed as a date, then 
     * the parse succeeds.  Clients may insist on strict adherence to the 
     * format by calling setLenient(false). 
     * 
     * <p> Note that the normal date formats associated with some calendars - such 
     * as the Chinese lunar calendar - do not specify enough fields to enable 
     * dates to be parsed unambiguously. In the case of the Chinese lunar 
     * calendar, while the year within the current 60-year cycle is specified, 
     * the number of such cycles since the start date of the calendar (in the 
     * ERA field of the Calendar object) is not normally part of the format, 
     * and parsing may assume the wrong era. For cases such as this it is 
     * recommended that clients parse using the parse method that takes a Calendar 
     * with the Calendar passed in set to the current date, or to a date 
     * within the era/cycle that should be assumed if absent in the format. 
     * 
     * @see #setLenient(boolean) 
     * 
     * @param text  The date/time string to be parsed 
     * 
     * @param pos   On input, the position at which to start parsing; on 
     *              output, the position at which parsing terminated, or the 
     *              start position if the parse failed. 
     * 
     * @return      A Date, or null if the input could not be parsed 
     * @stable ICU 2.0 
     */ 
    public Date parse(String text, ParsePosition pos) {
        Date result = null;
        int start = pos.getIndex();
        TimeZone tzsav = calendar.getTimeZone();
        calendar.clear();
        parse(text, calendar, pos);
        if (pos.getIndex() != start) {
            try { 
                result = calendar.getTime();
            } catch (IllegalArgumentException e) {
                // This occurs if the calendar is non-lenient and there is 
                // an out-of-range field.  We don't know which field was 
                // illegal so we set the error index to the start. 
                pos.setIndex(start);
                pos.setErrorIndex(start);
            } 
        } 
        // Restore TimeZone 
        calendar.setTimeZone(tzsav);
        return result;