java.text.ParsePosition Java Examples
The following examples show how to use
java.text.ParsePosition.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestReducedParser.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
@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)); }
Example #2
Source File: DateTimeFormatterBuilder.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * Match text with the prefix tree. * * @param text the input text to parse, not null * @param pos the position to start parsing at, from 0 to the text * length. Upon return, position will be updated to the new parse * position, or unchanged, if no match found. * @return the resulting string, or null if no match found. */ public String match(CharSequence text, ParsePosition pos) { int off = pos.getIndex(); int end = text.length(); if (!prefixOf(text, off, end)){ return null; } off += key.length(); if (child != null && off != end) { PrefixTree c = child; do { if (isEqual(c.c0, text.charAt(off))) { pos.setIndex(off); String found = c.match(text, pos); if (found != null) { return found; } break; } c = c.sibling; } while (c != null); } pos.setIndex(off); return value; }
Example #3
Source File: ExtendedMessageFormat.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Parse the format component of a format element. * * @param pattern string to parse * @param pos current parse position * @return Format description String */ private String parseFormatDescription(String pattern, ParsePosition pos) { int start = pos.getIndex(); seekNonWs(pattern, pos); int text = pos.getIndex(); int depth = 1; for (; pos.getIndex() < pattern.length(); next(pos)) { switch (pattern.charAt(pos.getIndex())) { case START_FE: depth++; break; case END_FE: depth--; if (depth == 0) { return pattern.substring(text, pos.getIndex()); } break; case QUOTE: getQuotedString(pattern, pos, false); break; } } throw new IllegalArgumentException( "Unterminated format element at position " + start); }
Example #4
Source File: TCKLocalizedFieldParser.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
@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); } }
Example #5
Source File: JSON.java From openapi-generator with Apache License 2.0 | 6 votes |
@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); } } }
Example #6
Source File: TestReducedParser.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
@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)); }
Example #7
Source File: RBBISymbolTable.java From fitnotifications with Apache License 2.0 | 6 votes |
public String parseReference(String text, ParsePosition pos, int limit) { int start = pos.getIndex(); int i = start; String result = ""; while (i < limit) { int c = UTF16.charAt(text, i); if ((i == start && !UCharacter.isUnicodeIdentifierStart(c)) || !UCharacter.isUnicodeIdentifierPart(c)) { break; } i += UTF16.getCharCount(c); } if (i == start) { // No valid name chars return result; // Indicate failure with empty string } pos.setIndex(i); result = text.substring(start, i); return result; }
Example #8
Source File: DateTimeFormatter.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * 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); }
Example #9
Source File: TestReducedParser.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
@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)); }
Example #10
Source File: StringUtils.java From EserKnife with Apache License 2.0 | 6 votes |
/** * 根据年月返回当月所有日期列表 * * @param year 年 * @param month 月 * @return 日期列表 */ public static Vector<String> createDateArray(int year, int month) { Vector<String> v = new Vector<String>(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); ParsePosition pos = new ParsePosition(0); Calendar cal = Calendar.getInstance(); cal.setTime(new Date(year - ONE_THOUSENT_NINE_HUNDRED, month - 1, TEN)); int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); String startTime = String.valueOf(year) + "-" + (month < TEN ? "0" + month : month) + "-01"; Date st = formatter.parse(startTime, pos); for (int i = 0; i < maxDay; i++) { if (i > 0) { st.setDate(st.getDate() + 1); } v.add(formatter.format(st)); } return v; }
Example #11
Source File: TCKLocalizedFieldParser.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
@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); } }
Example #12
Source File: TCKDateTimeFormatter.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Test public void test_parseUnresolved_StringParsePosition_duplicateFieldDifferentValue() { DateTimeFormatter test = new DateTimeFormatterBuilder() .appendValue(MONTH_OF_YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).toFormatter(); ParsePosition pos = new ParsePosition(3); TemporalAccessor result = test.parseUnresolved("XXX6-7", pos); assertEquals(pos.getIndex(), 3); assertEquals(pos.getErrorIndex(), 5); assertEquals(result, null); }
Example #13
Source File: TestDateTimeFormatterBuilder.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
@Test public void test_appendValue_subsequent2_parse3() throws Exception { builder.appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL).appendValue(DAY_OF_MONTH, 2); DateTimeFormatter f = builder.toFormatter(); assertEquals(f.toString(), "Value(MonthOfYear,1,2,NORMAL)Value(DayOfMonth,2)"); TemporalAccessor parsed = f.parseUnresolved("123", new ParsePosition(0)); assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L); assertEquals(parsed.getLong(DAY_OF_MONTH), 23L); }
Example #14
Source File: TestTextParser.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="parseStandaloneText") public void test_parseStandaloneText(Locale locale, TemporalField field, TextStyle style, int expectedValue, String input) { DateTimeFormatter formatter = getFormatter(field, style).withLocale(locale); ParsePosition pos = new ParsePosition(0); assertEquals(formatter.parseUnresolved(input, pos).getLong(field), (long) expectedValue); assertEquals(pos.getIndex(), input.length()); }
Example #15
Source File: TestZoneOffsetParser.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="offsets") public void test_parse_endStringMatch_EmptyUTC(String pattern, String parse, ZoneOffset expected) throws Exception { ParsePosition pos = new ParsePosition(5); TemporalAccessor parsed = getFormatter(pattern, "").parseUnresolved("OTHER" + parse, pos); assertEquals(pos.getIndex(), parse.length() + 5); assertParsed(parsed, expected); }
Example #16
Source File: TestDateTimeFormatterBuilder.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Test public void test_appendValueReduced() throws Exception { builder.appendValueReduced(YEAR, 2, 2, 2000); DateTimeFormatter f = builder.toFormatter(); assertEquals(f.toString(), "ReducedValue(Year,2,2,2000)"); TemporalAccessor parsed = f.parseUnresolved("12", new ParsePosition(0)); assertEquals(parsed.getLong(YEAR), 2012L); }
Example #17
Source File: TestTextParser.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="parseDayOfWeekText") public void test_parseDayOfWeekText(Locale locale, String pattern, String input, DayOfWeek expected) { DateTimeFormatter formatter = getPatternFormatter(pattern).withLocale(locale); ParsePosition pos = new ParsePosition(0); assertEquals(DayOfWeek.from(formatter.parse(input, pos)), expected); assertEquals(pos.getIndex(), input.length()); }
Example #18
Source File: TestDateTimeFormatterBuilder.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@Test public void test_appendValue_subsequent2_parse4() throws Exception { builder.appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL).appendValue(DAY_OF_MONTH, 2); DateTimeFormatter f = builder.toFormatter(); assertEquals(f.toString(), "Value(MonthOfYear,1,2,NORMAL)Value(DayOfMonth,2)"); TemporalAccessor parsed = f.parseUnresolved("0123", new ParsePosition(0)); assertEquals(parsed.getLong(MONTH_OF_YEAR), 1L); assertEquals(parsed.getLong(DAY_OF_MONTH), 23L); }
Example #19
Source File: TCKDateTimeFormatters.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="sample_isoDate") public void test_parse_isoDate( Integer year, Integer month, Integer day, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { Expected expected = createDate(year, month, day); if (offsetId != null) { expected.add(ZoneOffset.of(offsetId)); } assertParseMatch(DateTimeFormatter.ISO_DATE.parseUnresolved(input, new ParsePosition(0)), expected); } }
Example #20
Source File: NumberParser.java From poiji with MIT License | 5 votes |
private static Number parseNumber(String value, NumberFormat instance) { if (value == null || value.trim().isEmpty()) { throw new NumberFormatException(value); } ParsePosition pos = new ParsePosition(0); Number result = instance.parse(value, pos); if (isParsingError(pos, value)) { throw new NumberFormatException(value); } return result; }
Example #21
Source File: TestTextParser.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="parseText") public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception { if (input.equals(input.toUpperCase(Locale.ROOT))) { // Skip if the given input is all upper case (e.g., "Q1") return; } setCaseSensitive(true); ParsePosition pos = new ParsePosition(0); getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos); assertEquals(pos.getErrorIndex(), 0); }
Example #22
Source File: TCKDateTimeFormatter.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
@Test(expectedExceptions=NullPointerException.class) public void test_toFormat_parseObject_StringParsePosition_nullString() throws Exception { // SimpleDateFormat has this behavior DateTimeFormatter test = fmt.withLocale(Locale.ENGLISH).withDecimalStyle(DecimalStyle.STANDARD); Format format = test.toFormat(); ParsePosition pos = new ParsePosition(0); format.parseObject((String) null, pos); }
Example #23
Source File: UnicodeSet.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Parse a property pattern. * @param chars iterator over the pattern characters. Upon return * it will be advanced to the first character after the parsed * pattern, or the end of the iteration if all characters are * parsed. * @param rebuiltPat the pattern that was parsed, rebuilt or * copied from the input pattern, as appropriate. * @param symbols TODO */ private void applyPropertyPattern(RuleCharacterIterator chars, StringBuffer rebuiltPat, SymbolTable symbols) { String patStr = chars.lookahead(); ParsePosition pos = new ParsePosition(0); applyPropertyPattern(patStr, pos, symbols); if (pos.getIndex() == 0) { syntaxError(chars, "Invalid property pattern"); } chars.jumpahead(pos.getIndex()); rebuiltPat.append(patStr.substring(0, pos.getIndex())); }
Example #24
Source File: ValueMetaBase.java From hop with Apache License 2.0 | 5 votes |
protected synchronized Double convertStringToNumber( String string ) throws HopValueException { string = Const.trimToType( string, getTrimType() ); // see if trimming needs // to be performed before // conversion if ( Utils.isEmpty( string ) ) { return null; } try { DecimalFormat format = getDecimalFormat( false ); Number number; if ( lenientStringToNumber ) { number = format.parse( string ); } else { ParsePosition parsePosition = new ParsePosition( 0 ); number = format.parse( string, parsePosition ); if ( parsePosition.getIndex() < string.length() ) { throw new HopValueException( toString() + " : couldn't convert String to number : non-numeric character found at position " + ( parsePosition.getIndex() + 1 ) + " for value [" + string + "]" ); } } return new Double( number.doubleValue() ); } catch ( Exception e ) { throw new HopValueException( toString() + " : couldn't convert String to number ", e ); } }
Example #25
Source File: TCKDateTimeFormatters.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="sample_isoLocalTime") public void test_parse_isoLocalTime( Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { Expected expected = createTime(hour, min, sec, nano); // offset/zone not expected to be parsed assertParseMatch(DateTimeFormatter.ISO_LOCAL_TIME.parseUnresolved(input, new ParsePosition(0)), expected); } }
Example #26
Source File: TestTextParser.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public void test_parse_noMatch2() throws Exception { ParsePosition pos = new ParsePosition(3); TemporalAccessor parsed = getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos); assertEquals(pos.getErrorIndex(), 3); assertEquals(parsed, null); }
Example #27
Source File: TCKDateTimeFormatters.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="sample_isoTime") public void test_parse_isoTime( Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { Expected expected = createTime(hour, min, sec, nano); if (offsetId != null) { expected.add(ZoneOffset.of(offsetId)); } assertParseMatch(DateTimeFormatter.ISO_TIME.parseUnresolved(input, new ParsePosition(0)), expected); } }
Example #28
Source File: TCKDateTimeFormatters.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="sample_isoOffsetDate") public void test_parse_isoOffsetDate( Integer year, Integer month, Integer day, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { Expected expected = createDate(year, month, day); buildCalendrical(expected, offsetId, null); // zone not expected to be parsed assertParseMatch(DateTimeFormatter.ISO_OFFSET_DATE.parseUnresolved(input, new ParsePosition(0)), expected); } }
Example #29
Source File: TestTextParser.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public void test_parse_french_short_strict_full_noMatch() throws Exception { setStrict(true); ParsePosition pos = new ParsePosition(0); getFormatter(MONTH_OF_YEAR, TextStyle.SHORT).withLocale(Locale.FRENCH) .parseUnresolved("janvier", pos); assertEquals(pos.getErrorIndex(), 0); }
Example #30
Source File: TCKDateTimeFormatters.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider="sample_isoOffsetDateTime") public void test_parse_isoOffsetDateTime( Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { Expected expected = createDateTime(year, month, day, hour, min, sec, nano); buildCalendrical(expected, offsetId, null); // zone not expected to be parsed assertParseMatch(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parseUnresolved(input, new ParsePosition(0)), expected); } }