本文整理匯總了Java中java.text.ParsePosition.getErrorIndex方法的典型用法代碼示例。如果您正苦於以下問題:Java ParsePosition.getErrorIndex方法的具體用法?Java ParsePosition.getErrorIndex怎麽用?Java ParsePosition.getErrorIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.text.ParsePosition
的用法示例。
在下文中一共展示了ParsePosition.getErrorIndex方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test_parse_fresh
import java.text.ParsePosition; //導入方法依賴的package包/類
@Test(dataProvider="parseData")
public void test_parse_fresh(int minWidth, int maxWidth, SignStyle signStyle, int subsequentWidth, String text, int pos, int expectedPos, long expectedValue) {
ParsePosition ppos = new ParsePosition(pos);
DateTimeFormatter dtf = getFormatter(DAY_OF_MONTH, minWidth, maxWidth, signStyle);
if (subsequentWidth > 0) {
// hacky, to reserve space
dtf = builder.appendValue(DAY_OF_YEAR, subsequentWidth).toFormatter(locale).withDecimalStyle(decimalStyle);
}
TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getErrorIndex(), expectedPos);
} else {
assertTrue(subsequentWidth >= 0);
assertEquals(ppos.getIndex(), expectedPos + subsequentWidth);
assertEquals(parsed.getLong(DAY_OF_MONTH), expectedValue);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
示例2: test_parseAdjacent
import java.text.ParsePosition; //導入方法依賴的package包/類
@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));
}
}
示例3: parseObject
import java.text.ParsePosition; //導入方法依賴的package包/類
/**
* Parses a string to produce an object.
*
* @exception ParseException if the specified string is invalid.
*/
@Override
public Object parseObject(
String source) throws ParseException
{
ParsePosition status = new ParsePosition(0);
Object result = parseObject(source, status);
int index = status.getIndex();
if (index == 0 ||
(source != null && index < source.length()))
{
throw new ParseException(_LOG.getMessage(
"FORMAT_PARSEOBJECT_FAIL"), status.getErrorIndex());
}
return result;
}
示例4: parse
import java.text.ParsePosition; //導入方法依賴的package包/類
@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;
}
示例5: parseResolved0
import java.text.ParsePosition; //導入方法依賴的package包/類
/**
* 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);
}
示例6: test_parse_textLocalDate
import java.text.ParsePosition; //導入方法依賴的package包/類
@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);
}
}
示例7: parse
import java.text.ParsePosition; //導入方法依賴的package包/類
/**
* <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;
}
示例8: test_parseSignsStrict
import java.text.ParsePosition; //導入方法依賴的package包/類
@Test(dataProvider="parseSignsStrict")
public void test_parseSignsStrict(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception {
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);
}
}
示例9: test_parseStrict_baseDate
import java.text.ParsePosition; //導入方法依賴的package包/類
@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);
}
}
示例10: test_parse_WeekBasedYear
import java.text.ParsePosition; //導入方法依賴的package包/類
@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);
}
}
示例11: main
import java.text.ParsePosition; //導入方法依賴的package包/類
public static void main(String[] args) {
String date = "13 Jan 2005 21:45:34 ABC";
String format = "dd MMM yyyy HH:mm:ss z";
ParsePosition pp = new ParsePosition(0);
pp.setIndex(0);
SimpleDateFormat sd = new SimpleDateFormat(format, Locale.ENGLISH);
Date d = sd.parse(date, pp);
int errorIndex = pp.getErrorIndex();
if (errorIndex == 21) {
System.out.println(": passed");
} else {
System.out.println(": failed");
throw new RuntimeException("Failed with wrong index: " + errorIndex);
}
}
示例12: test_parse_textField
import java.text.ParsePosition; //導入方法依賴的package包/類
@Test(dataProvider="FieldPatterns")
public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) {
WeekFields weekDef = WeekFields.of(locale);
TemporalField field = null;
switch(pattern.charAt(0)) {
case 'c' :
case 'e' :
field = weekDef.dayOfWeek();
break;
case 'w':
field = weekDef.weekOfWeekBasedYear();
break;
case 'W':
field = weekDef.weekOfMonth();
break;
case 'Y':
field = weekDef.weekBasedYear();
break;
default:
throw new IllegalStateException("bad format letter from pattern");
}
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");
long value = parsed.getLong(field);
assertEquals(value, expectedValue, "Value incorrect for " + field);
}
}
示例13: test_parseDigitsLenient
import java.text.ParsePosition; //導入方法依賴的package包/類
@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: test_parse_success
import java.text.ParsePosition; //導入方法依賴的package包/類
@Test(dataProvider="success")
public void test_parse_success(char c, boolean caseSensitive,
String text, int pos, int expectedPos) {
setCaseSensitive(caseSensitive);
ParsePosition ppos = new ParsePosition(pos);
TemporalAccessor parsed = getFormatter(c).parseUnresolved(text, ppos);
if (ppos.getErrorIndex() != -1) {
assertEquals(ppos.getIndex(), expectedPos);
} else {
assertEquals(ppos.getIndex(), expectedPos);
assertEquals(parsed.isSupported(YEAR), false);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
示例15: parse
import java.text.ParsePosition; //導入方法依賴的package包/類
/**
* Parses text from the beginning of the given string to produce a number.
* The method might not use the entire text of the given string.
*
* @param text A String whose beginning should be parsed.
* @return A Number parsed from the string.
* @throws ParseException if the beginning of the specified string
* cannot be parsed.
* @see #format
* @stable ICU 2.0
*/
//Bug 4375399 [Richard/GCL]
public Number parse(String text) throws ParseException {
ParsePosition parsePosition = new ParsePosition(0);
Number result = parse(text, parsePosition);
if (parsePosition.getIndex() == 0) {
throw new ParseException("Unparseable number: \"" + text + '"',
parsePosition.getErrorIndex());
}
return result;
}