本文整理汇总了Java中java.text.ParsePosition类的典型用法代码示例。如果您正苦于以下问题:Java ParsePosition类的具体用法?Java ParsePosition怎么用?Java ParsePosition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ParsePosition类属于java.text包,在下文中一共展示了ParsePosition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import java.text.ParsePosition; //导入依赖的package包/类
/**
* Convert a String into a <code>Number</code> object.
* @param sourceType the source type of the conversion
* @param targetType The type to convert the value to
* @param value The String date value.
* @param format The NumberFormat to parse the String value.
*
* @return The converted Number object.
* @throws ConversionException if the String cannot be converted.
*/
private Number parse(final Class<?> sourceType, final Class<?> targetType, final String value, final NumberFormat format) {
final ParsePosition pos = new ParsePosition(0);
final Number parsedNumber = format.parse(value, pos);
if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedNumber == null) {
String msg = "Error converting from '" + toString(sourceType) + "' to '" + toString(targetType) + "'";
if (format instanceof DecimalFormat) {
msg += " using pattern '" + ((DecimalFormat)format).toPattern() + "'";
}
if (locale != null) {
msg += " for locale=[" + locale + "]";
}
if (log().isDebugEnabled()) {
log().debug(" " + msg);
}
throw new ConversionException(msg);
}
return parsedNumber;
}
示例2: test_reducedWithChronoYear
import java.text.ParsePosition; //导入依赖的package包/类
@Test(dataProvider="ReducedWithChrono")
public void test_reducedWithChronoYear(ChronoLocalDate date) {
Chronology chrono = date.getChronology();
DateTimeFormatter df
= new DateTimeFormatterBuilder().appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
.toFormatter()
.withChronology(chrono);
int expected = date.get(YEAR);
String input = df.format(date);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
int actual = parsed.get(YEAR);
assertEquals(actual, expected,
String.format("Wrong date parsed, chrono: %s, input: %s",
chrono, input));
}
示例3: processAndAddData
import java.text.ParsePosition; //导入依赖的package包/类
private void processAndAddData(final Realm realm, final String sectionKey, final List<NYTimesStory> stories) {
if (stories.isEmpty()) return;
realm.executeTransactionAsync(r -> {
for (NYTimesStory story : stories) {
Date parsedPublishedDate = inputDateFormat.parse(story.getPublishedDate(), new ParsePosition(0));
story.setSortTimeStamp(parsedPublishedDate.getTime());
story.setPublishedDate(outputDateFormat.format(parsedPublishedDate));
// Find existing story in Realm (if any)
// If it exists, we need to merge the local state with the remote, because the local state
// contains more info than is available on the server.
NYTimesStory persistedStory = r.where(NYTimesStory.class).equalTo(NYTimesStory.URL, story.getUrl()).findFirst();
if (persistedStory != null) {
// Only local state is the `read` boolean.
story.setRead(persistedStory.isRead());
}
// Only create or update the local story if needed
if (persistedStory == null || !persistedStory.getUpdatedDate().equals(story.getUpdatedDate())) {
story.setApiSection(sectionKey);
r.copyToRealmOrUpdate(story);
}
}
}, throwable -> Timber.e(throwable, "Could not save data"));
}
示例4: test_parse_textField
import java.text.ParsePosition; //导入依赖的package包/类
@Test(dataProvider="parseData")
public void test_parse_textField(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_WEEK, 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_WEEK), expectedValue);
assertEquals(parsed.query(TemporalQueries.chronology()), null);
assertEquals(parsed.query(TemporalQueries.zoneId()), null);
}
}
示例5: testParseRfc3339Examples
import java.text.ParsePosition; //导入依赖的package包/类
public void testParseRfc3339Examples() throws java.text.ParseException {
// Two digit milliseconds.
Date d = ISO8601Utils.parse("1985-04-12T23:20:50.52Z", new ParsePosition(0));
assertEquals(newDate(1985, 4, 12, 23, 20, 50, 520, 0), d);
d = ISO8601Utils.parse("1996-12-19T16:39:57-08:00", new ParsePosition(0));
assertEquals(newDate(1996, 12, 19, 16, 39, 57, 0, -8 * 60), d);
// Truncated leap second.
d = ISO8601Utils.parse("1990-12-31T23:59:60Z", new ParsePosition(0));
assertEquals(newDate(1990, 12, 31, 23, 59, 59, 0, 0), d);
// Truncated leap second.
d = ISO8601Utils.parse("1990-12-31T15:59:60-08:00", new ParsePosition(0));
assertEquals(newDate(1990, 12, 31, 15, 59, 59, 0, -8 * 60), d);
// Two digit milliseconds.
d = ISO8601Utils.parse("1937-01-01T12:00:27.87+00:20", new ParsePosition(0));
assertEquals(newDate(1937, 1, 1, 12, 0, 27, 870, 20), d);
}
示例6: 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);
}
示例7: test_reducedWithChronoYearOfEra
import java.text.ParsePosition; //导入依赖的package包/类
@Test(dataProvider="ReducedWithChrono")
public void test_reducedWithChronoYearOfEra(ChronoLocalDate date) {
Chronology chrono = date.getChronology();
DateTimeFormatter df
= new DateTimeFormatterBuilder().appendValueReduced(YEAR_OF_ERA, 2, 2, LocalDate.of(2000, 1, 1))
.toFormatter()
.withChronology(chrono);
int expected = date.get(YEAR_OF_ERA);
String input = df.format(date);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
int actual = parsed.get(YEAR_OF_ERA);
assertEquals(actual, expected,
String.format("Wrong date parsed, chrono: %s, input: %s",
chrono, input));
}
示例8: test_reducedWithLateChronoChange
import java.text.ParsePosition; //导入依赖的package包/类
@Test
public void test_reducedWithLateChronoChange() {
ThaiBuddhistDate date = ThaiBuddhistDate.of(2543, 1, 1);
DateTimeFormatter df
= new DateTimeFormatterBuilder()
.appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
.appendLiteral(" ")
.appendChronologyId()
.toFormatter();
int expected = date.get(YEAR);
String input = df.format(date);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
assertEquals(pos.getIndex(), input.length(), "Input not parsed completely");
assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)");
int actual = parsed.get(YEAR);
assertEquals(actual, expected,
String.format("Wrong date parsed, chrono: %s, input: %s",
parsed.query(TemporalQueries.chronology()), input));
}
示例9: read
import java.text.ParsePosition; //导入依赖的package包/类
@Override
public java.sql.Date read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
try {
if (dateFormat != null) {
return new java.sql.Date(dateFormat.parse(date).getTime());
}
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
示例10: parseReference
import java.text.ParsePosition; //导入依赖的package包/类
/**
* Implement SymbolTable API. Parse out a symbol reference
* name.
*/
@Override
public String parseReference(String text, ParsePosition pos, int limit) {
int start = pos.getIndex();
int i = start;
while (i < limit) {
char c = text.charAt(i);
if ((i==start && !UCharacter.isUnicodeIdentifierStart(c)) ||
!UCharacter.isUnicodeIdentifierPart(c)) {
break;
}
++i;
}
if (i == start) { // No valid name chars
return null;
}
pos.setIndex(i);
return text.substring(start, i);
}
示例11: test_parse_CharSequence_ParsePosition_resolved
import java.text.ParsePosition; //导入依赖的package包/类
@Test
public void test_parse_CharSequence_ParsePosition_resolved() {
DateTimeFormatter test = DateTimeFormatter.ISO_DATE;
ParsePosition pos = new ParsePosition(3);
TemporalAccessor result = test.parse("XXX2012-06-30XXX", pos);
assertEquals(pos.getIndex(), 13);
assertEquals(pos.getErrorIndex(), -1);
assertEquals(result.isSupported(YEAR), true);
assertEquals(result.isSupported(MONTH_OF_YEAR), true);
assertEquals(result.isSupported(DAY_OF_MONTH), true);
assertEquals(result.isSupported(HOUR_OF_DAY), false);
assertEquals(result.getLong(YEAR), 2012L);
assertEquals(result.getLong(MONTH_OF_YEAR), 6L);
assertEquals(result.getLong(DAY_OF_MONTH), 30L);
assertEquals(result.query(LocalDate::from), LocalDate.of(2012, 6, 30));
}
示例12: test_reducedWithLateChronoChangeTwice
import java.text.ParsePosition; //导入依赖的package包/类
@Test
public void test_reducedWithLateChronoChangeTwice() {
DateTimeFormatter df
= new DateTimeFormatterBuilder()
.appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1))
.appendLiteral(" ")
.appendChronologyId()
.appendLiteral(" ")
.appendChronologyId()
.toFormatter();
int expected = 2044;
String input = "44 ThaiBuddhist ISO";
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
assertEquals(pos.getIndex(), input.length(), "Input not parsed completely: " + pos);
assertEquals(pos.getErrorIndex(), -1, "Error index should be -1 (no-error)");
int actual = parsed.get(YEAR);
assertEquals(actual, expected,
String.format("Wrong date parsed, chrono: %s, input: %s",
parsed.query(TemporalQueries.chronology()), input));
}
示例13: test_parse_bigOffsets
import java.text.ParsePosition; //导入依赖的package包/类
@Test(dataProvider="bigOffsets")
public void test_parse_bigOffsets(String pattern, String parse, long offsetSecs) throws Exception {
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = getFormatter(pattern, "").parseUnresolved(parse, pos);
assertEquals(pos.getIndex(), parse.length());
assertEquals(parsed.getLong(OFFSET_SECONDS), offsetSecs);
}
示例14: test_parse_noMatch_atEnd
import java.text.ParsePosition; //导入依赖的package包/类
public void test_parse_noMatch_atEnd() throws Exception {
ParsePosition pos = new ParsePosition(6);
TemporalAccessor parsed =
getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos);
assertEquals(pos.getErrorIndex(), 6);
assertEquals(parsed, null);
}
示例15: createDecimalFormatter
import java.text.ParsePosition; //导入依赖的package包/类
private static TextFormatter<String> createDecimalFormatter() {
DecimalFormat format = new DecimalFormat("#.0");
format.setNegativePrefix("-");
return new TextFormatter<>(c -> {
if (c.getControlNewText().isEmpty()) return c;
ParsePosition pos = new ParsePosition(0);
Number result = format.parse(c.getControlNewText(), pos);
if (result == null || pos.getIndex() < c.getControlNewText().length()) {
return null;
} else return c;
});
}