java.text.ParsePosition#getErrorIndex ( )源码实例Demo

下面列出了java.text.ParsePosition#getErrorIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public Number parse(String text, Locale locale) throws ParseException {
	NumberFormat format = getNumberFormat(locale);
	ParsePosition position = new ParsePosition(0);
	Number number = format.parse(text, position);
	if (position.getErrorIndex() != -1) {
		throw new ParseException(text, position.getIndex());
	}
	if (!this.lenient) {
		if (text.length() != position.getIndex()) {
			// indicates a part of the string that was not parsed
			throw new ParseException(text, position.getIndex());
		}
	}
	return number;
}
 
源代码2 项目: TencentKona-8   文件: TCKLocalizedFieldParser.java
@Test(dataProvider="LocalWeekMonthYearPatterns")
public void test_parse_textLocalDate(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        assertEquals(parsed.isSupported(YEAR_OF_ERA), true);
        assertEquals(parsed.isSupported(WeekFields.of(locale).dayOfWeek()), true);
        assertEquals(parsed.isSupported(WeekFields.of(locale).weekOfMonth()) ||
                parsed.isSupported(WeekFields.of(locale).weekOfYear()), true);
        // ensure combination resolves into a date
        LocalDate result = LocalDate.parse(text, dtf);
        assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern);
    }
}
 
源代码3 项目: jdk8u-jdk   文件: TCKLocalizedFieldParser.java
@Test(dataProvider="LocalWeekBasedYearPatterns")
public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        WeekFields weekDef = WeekFields.of(locale);
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0);
        // ensure combination resolves into a date
        LocalDate result = LocalDate.parse(text, dtf);
        assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef);
    }
}
 
源代码4 项目: dragonwell8_jdk   文件: DateTimeFormatter.java
/**
 * Parses and resolves the specified text.
 * <p>
 * This parses to a {@code TemporalAccessor} ensuring that the text is fully parsed.
 *
 * @param text  the text to parse, not null
 * @param position  the position to parse from, updated with length parsed
 *  and the index of any error, null if parsing whole string
 * @return the resolved result of the parse, not null
 * @throws DateTimeParseException if the parse fails
 * @throws DateTimeException if an error occurs while resolving the date or time
 * @throws IndexOutOfBoundsException if the position is invalid
 */
private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) {
    ParsePosition pos = (position != null ? position : new ParsePosition(0));
    DateTimeParseContext context = parseUnresolved0(text, pos);
    if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
        String abbr;
        if (text.length() > 64) {
            abbr = text.subSequence(0, 64).toString() + "...";
        } else {
            abbr = text.toString();
        }
        if (pos.getErrorIndex() >= 0) {
            throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " +
                    pos.getErrorIndex(), text, pos.getErrorIndex());
        } else {
            throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " +
                    pos.getIndex(), text, pos.getIndex());
        }
    }
    return context.toResolved(resolverStyle, resolverFields);
}
 
/**
 * <p>Parse the value with the specified <code>Format</code>.</p>
 *
 * @param value The value to be parsed.
 * @param formatter The Format to parse the value with.
 * @return The parsed value if valid or <code>null</code> if invalid.
 */
protected Object parse(String value, Format formatter) {

    ParsePosition pos = new ParsePosition(0);
    Object parsedValue = formatter.parseObject(value, pos);
    if (pos.getErrorIndex() > -1) {
        return null;
    }

    if (isStrict() && pos.getIndex() < value.length()) {
        return null;
    }

    if (parsedValue != null) {
        parsedValue = processParsedValue(parsedValue, formatter);
    }

    return parsedValue;

}
 
源代码6 项目: lams   文件: StdDateFormat.java
@Override
public Date parse(String dateStr) throws ParseException
{
    dateStr = dateStr.trim();
    ParsePosition pos = new ParsePosition(0);
    Date dt = _parseDate(dateStr, pos);
    if (dt != null) {
        return dt;
    }
    StringBuilder sb = new StringBuilder();
    for (String f : ALL_FORMATS) {
        if (sb.length() > 0) {
            sb.append("\", \"");
        } else {
            sb.append('"');
        }
        sb.append(f);
    }
    sb.append('"');
    throw new ParseException
        (String.format("Cannot parse date \"%s\": not compatible with any of standard forms (%s)",
                       dateStr, sb.toString()), pos.getErrorIndex());
}
 
源代码7 项目: lams   文件: AbstractNumberFormatter.java
@Override
public Number parse(String text, Locale locale) throws ParseException {
	NumberFormat format = getNumberFormat(locale);
	ParsePosition position = new ParsePosition(0);
	Number number = format.parse(text, position);
	if (position.getErrorIndex() != -1) {
		throw new ParseException(text, position.getIndex());
	}
	if (!this.lenient) {
		if (text.length() != position.getIndex()) {
			// indicates a part of the string that was not parsed
			throw new ParseException(text, position.getIndex());
		}
	}
	return number;
}
 
源代码8 项目: openjdk-jdk8u   文件: TCKLocalizedFieldParser.java
@Test(dataProvider="LocalWeekBasedYearPatterns")
public void test_parse_WeekBasedYear(String pattern, String text, int pos, int expectedPos, LocalDate expectedValue) {
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        WeekFields weekDef = WeekFields.of(locale);
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        assertEquals(parsed.isSupported(weekDef.dayOfWeek()), pattern.indexOf('e') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekOfWeekBasedYear()), pattern.indexOf('w') >= 0);
        assertEquals(parsed.isSupported(weekDef.weekBasedYear()), pattern.indexOf('Y') >= 0);
        // ensure combination resolves into a date
        LocalDate result = LocalDate.parse(text, dtf);
        assertEquals(result, expectedValue, "LocalDate incorrect for " + pattern + ", weekDef: " + weekDef);
    }
}
 
源代码9 项目: TencentKona-8   文件: TestReducedParser.java
@Test(dataProvider="ParseAdjacent")
public void test_parseAdjacent(String pattern, String input, boolean strict, int pos, int parseLen, int year, int month, int day) {
    ParsePosition ppos = new ParsePosition(0);
    builder = new DateTimeFormatterBuilder();
    setStrict(strict);
    builder.appendPattern(pattern);
    DateTimeFormatter dtf = builder.toFormatter();

    TemporalAccessor parsed = dtf.parseUnresolved(input, ppos);
    assertNotNull(parsed, String.format("parse failed: ppos: %s, formatter: %s%n", ppos.toString(), dtf));
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR_OF_ERA, Long.valueOf(year));
        assertParsed(parsed, MONTH_OF_YEAR, Long.valueOf(month));
        assertParsed(parsed, DAY_OF_MONTH, Long.valueOf(day));
    }
}
 
源代码10 项目: jdk8u60   文件: DateTimeFormatter.java
/**
 * Parses and resolves the specified text.
 * <p>
 * This parses to a {@code TemporalAccessor} ensuring that the text is fully parsed.
 *
 * @param text  the text to parse, not null
 * @param position  the position to parse from, updated with length parsed
 *  and the index of any error, null if parsing whole string
 * @return the resolved result of the parse, not null
 * @throws DateTimeParseException if the parse fails
 * @throws DateTimeException if an error occurs while resolving the date or time
 * @throws IndexOutOfBoundsException if the position is invalid
 */
private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) {
    ParsePosition pos = (position != null ? position : new ParsePosition(0));
    DateTimeParseContext context = parseUnresolved0(text, pos);
    if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
        String abbr;
        if (text.length() > 64) {
            abbr = text.subSequence(0, 64).toString() + "...";
        } else {
            abbr = text.toString();
        }
        if (pos.getErrorIndex() >= 0) {
            throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " +
                    pos.getErrorIndex(), text, pos.getErrorIndex());
        } else {
            throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " +
                    pos.getIndex(), text, pos.getIndex());
        }
    }
    return context.toResolved(resolverStyle, resolverFields);
}
 
源代码11 项目: openjdk-jdk8u-backup   文件: DateTimeFormatter.java
/**
 * Parses and resolves the specified text.
 * <p>
 * This parses to a {@code TemporalAccessor} ensuring that the text is fully parsed.
 *
 * @param text  the text to parse, not null
 * @param position  the position to parse from, updated with length parsed
 *  and the index of any error, null if parsing whole string
 * @return the resolved result of the parse, not null
 * @throws DateTimeParseException if the parse fails
 * @throws DateTimeException if an error occurs while resolving the date or time
 * @throws IndexOutOfBoundsException if the position is invalid
 */
private TemporalAccessor parseResolved0(final CharSequence text, final ParsePosition position) {
    ParsePosition pos = (position != null ? position : new ParsePosition(0));
    DateTimeParseContext context = parseUnresolved0(text, pos);
    if (context == null || pos.getErrorIndex() >= 0 || (position == null && pos.getIndex() < text.length())) {
        String abbr;
        if (text.length() > 64) {
            abbr = text.subSequence(0, 64).toString() + "...";
        } else {
            abbr = text.toString();
        }
        if (pos.getErrorIndex() >= 0) {
            throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " +
                    pos.getErrorIndex(), text, pos.getErrorIndex());
        } else {
            throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " +
                    pos.getIndex(), text, pos.getIndex());
        }
    }
    return context.toResolved(resolverStyle, resolverFields);
}
 
源代码12 项目: dragonwell8_jdk   文件: TestNumberParser.java
@Test(dataProvider="parseSignsLenient")
public void test_parseSignsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
    setStrict(false);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
    if (pos.getErrorIndex() != -1) {
        assertEquals(pos.getErrorIndex(), parseLen);
    } else {
        assertEquals(pos.getIndex(), parseLen);
        assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}
 
源代码13 项目: dragonwell8_jdk   文件: TestNumberParser.java
@Test(dataProvider="parseDigitsLenient")
public void test_parseDigitsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
    setStrict(false);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
    if (pos.getErrorIndex() != -1) {
        assertEquals(pos.getErrorIndex(), parseLen);
    } else {
        assertEquals(pos.getIndex(), parseLen);
        assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}
 
源代码14 项目: openjdk-jdk9   文件: TestReducedParser.java
@Test(dataProvider="ParseLenientSensitive")
public void test_parseLenient_baseDate(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos,
                              Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(false);
    TemporalAccessor parsed = getFormatterBaseDate(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), lenient.parseLen, "error case parse position");
        assertEquals(parsed, lenient.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), lenient.parseLen, "parse position");
        assertParsed(parsed, YEAR, lenient.parseVal != null ? (long) lenient.parseVal : null);
    }
}
 
源代码15 项目: openjdk-jdk8u   文件: TestReducedParser.java
@Test(dataProvider="ParseLenientSensitive")
public void test_parseStrict(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos,
    Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), strict.parseLen, "error case parse position");
        assertEquals(parsed, strict.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), strict.parseLen, "parse position");
        assertParsed(parsed, YEAR, strict.parseVal != null ? (long) strict.parseVal : null);
    }
}
 
源代码16 项目: jdk8u60   文件: TestNumberParser.java
@Test(dataProvider="parseDigitsLenient")
public void test_parseDigitsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
    setStrict(false);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos);
    if (pos.getErrorIndex() != -1) {
        assertEquals(pos.getErrorIndex(), parseLen);
    } else {
        assertEquals(pos.getIndex(), parseLen);
        assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}
 
源代码17 项目: openjdk-jdk8u   文件: TestReducedParser.java
@Test(dataProvider="ParseLenientSensitive")
public void test_parseStrict_baseDate(TemporalField field, int minWidth, int maxWidth, int baseValue, String input, int pos,
                             Pair strict, Pair lenient) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatterBaseDate(field, minWidth, maxWidth, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), strict.parseLen, "error case parse position");
        assertEquals(parsed, strict.parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), strict.parseLen, "parse position");
        assertParsed(parsed, YEAR, strict.parseVal != null ? (long) strict.parseVal : null);
    }
}
 
源代码18 项目: TencentKona-8   文件: TestReducedParser.java
@Test(dataProvider="ParseAll")
public void test_parseAllStrict(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
        assertEquals(parsed, parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
    }
}
 
源代码19 项目: dragonwell8_jdk   文件: TestReducedParser.java
@Test(dataProvider="ParseAll")
public void test_parseAllStrict(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(true);
    TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
        assertEquals(parsed, parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
    }
}
 
源代码20 项目: dragonwell8_jdk   文件: TestReducedParser.java
@Test(dataProvider="ParseAll")
public void test_parseAllLenient(TemporalField field, int width, int baseValue, String input, int pos, int parseLen, Integer parseVal) {
    ParsePosition ppos = new ParsePosition(pos);
    setStrict(false);
    TemporalAccessor parsed = getFormatter0(field, width, baseValue).parseUnresolved(input, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), parseLen, "error case parse position");
        assertEquals(parsed, parseVal, "unexpected parse result");
    } else {
        assertEquals(ppos.getIndex(), parseLen, "parse position");
        assertParsed(parsed, YEAR, parseVal != null ? (long) parseVal : null);
    }
}