public static boolean isNumeric(String str) { NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos); return str.length() == pos.getIndex(); }
@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; }
private int parseNumber(int max, String string, int offset, int field, int skew) { ParsePosition position = new ParsePosition(offset); Number result = parseNumber(max, string, position); if (result == null) { return -position.getErrorIndex() - 1; } calendar.set(field, result.intValue() + skew); return position.getIndex(); }
Exception fail = null; try { int offset = pos.getIndex(); Calendar calendar = new GregorianCalendar(year, month - 1, day); pos.setIndex(offset); return calendar.getTime(); calendar.set(Calendar.MILLISECOND, milliseconds); pos.setIndex(offset); return calendar.getTime(); msg = "("+fail.getClass().getName()+")"; ParseException ex = new ParseException("Failed to parse date " + input + ": " + msg, pos.getIndex()); ex.initCause(fail); throw ex;
StringBuilder dateText = new StringBuilder( text.substring( pos.getIndex() ) ); ParsePosition positionError = new ParsePosition( 0 ); tempDate = super.parse( dateText.toString(), positionError ); if ( tempDate != null ) { pos.setErrorIndex( pos.getIndex() ); return null; int startNanosecondsPosition = positionError.getErrorIndex(); int endNanosecondsPosition = endNanosecondPatternPosition - startNanosecondPatternPosition + 1 + startNanosecondsPosition; String nanoseconds = String.valueOf( dateText.subSequence( startNanosecondsPosition, endNanosecondsPosition ) ); dateText.delete( startNanosecondsPosition, endNanosecondsPosition ); ParsePosition position = new ParsePosition( 0 ); dateText.append( NANOSECOND_PLACEHOLDER ); tempDate = super.parse( dateText.toString(), position ); if ( tempDate == null ) { pos.setErrorIndex( position.getErrorIndex() ); return null; String result = timestampFormatDate + '.' + nanoseconds; Timestamp res = Timestamp.valueOf( timestampFormatDate + '.' + nanoseconds ); pos.setIndex( pos.getIndex() + result.length() ); return res;
@Override boolean parse(final FastDateParser parser, final Calendar calendar, final String source, final ParsePosition pos, final int maxWidth) { final Matcher matcher = pattern.matcher(source.substring(pos.getIndex())); if (!matcher.lookingAt()) { pos.setErrorIndex(pos.getIndex()); return false; } pos.setIndex(pos.getIndex() + matcher.end(1)); setCalendar(parser, calendar, matcher.group(1)); return true; }
private Date parseDate(String str, String[] parsePatterns, Locale locale) throws ParseException { if ((str == null) || (parsePatterns == null)) { throw new IllegalArgumentException("Date and Patterns must not be null"); } SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0], locale); } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(str, pos); if ((date != null) && (pos.getIndex() == str.length())) { return date; } } throw new ParseException("Unable to parse the date: " + str, -1); } }
ParsePosition pos = new ParsePosition(0); Date time = f.getFormat().parse(computedValue, pos); if (pos.getIndex() == computedValue.length()) { DateRange range = f.expand(time); if (range.getMinValue().equals(range.getMaxValue())) { throw new ParseException("Invalid date: " + value, 0);
final void parseFoldingWhiteSpace() throws ParseException { if (!skipFoldingWhiteSpace()) { throw new ParseException("Invalid input: expected FWS", pos.getIndex()); } }
ParsePosition position = new ParsePosition(0); Date result = STANDARD_DATE_FORMAT.get().parse(value, position); if (position.getIndex() == value.length()) { BROWSER_COMPATIBLE_DATE_FORMATS[i] = format; position.setIndex(0); result = format.parse(value, position); if (position.getIndex() != 0) {
/** * Returns a {@link ParseException} with a "Unparsable string" message. The error message is * built from the specified string starting at the specified position. Properties {@link * ParsePosition#getIndex} and {@link ParsePosition#getErrorIndex} must be accurate before this * method is invoked. * * @param text The unparsable string. * @param position The position in the string. * @return An exception with a formatted error message. */ private ParseException unparsableString(final String text, final ParsePosition position) { final int errorIndex = position.getErrorIndex(); String message = LoggedFormat.formatUnparsable(text, position.getIndex(), errorIndex, null); message = complete(message); return trim("unparsableString", new ParseException(message, errorIndex)); }
/** * Moves to the next non-whitespace character and checks if this character is the specified * separator. If the separator is found, it is skipped. Otherwise, this method thrown a {@link * ParseException}. * * @param text The text to parse. * @param position In input, the position where to start parsing from. In output, the first * character after the separator. * @param separator The character to search. * @throws ParseException if the separator was not found. */ private void parseSeparator( final String text, final ParsePosition position, final char separator) throws ParseException { if (!parseOptionalSeparator(text, position, separator)) { position.setErrorIndex(position.getIndex()); throw unparsableString(text, position); } }
@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; }
/** {@inheritDoc} */ @Override public Vector1D parse(final String source) throws MathParseException { ParsePosition parsePosition = new ParsePosition(0); Vector1D result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Vector1D.class); } return result; }
Exception fail = null; try { int offset = pos.getIndex(); Calendar calendar = new GregorianCalendar(year, month - 1, day); pos.setIndex(offset); return calendar.getTime(); calendar.set(Calendar.MILLISECOND, milliseconds); pos.setIndex(offset); return calendar.getTime(); msg = "("+fail.getClass().getName()+")"; ParseException ex = new ParseException("Failed to parse date " + input + ": " + msg, pos.getIndex()); ex.initCause(fail); throw ex;
return EmptyArray.OBJECT; ParsePosition internalPos = new ParsePosition(0); int offset = position.getIndex(); Object[] result = new Object[maxArgumentIndex + 1]; for (int i = 0; i <= maxOffset; i++) { String sub = strings[i]; if (!string.startsWith(sub, offset)) { position.setErrorIndex(offset); return null; int next = string.indexOf(strings[i + 1], offset); if (next == -1) { position.setErrorIndex(offset); return null; internalPos.setIndex(offset); parse = format.parseObject(string, internalPos); if (internalPos.getErrorIndex() != -1) { position.setErrorIndex(offset); return null; offset = internalPos.getIndex(); position.setIndex(offset); return result;
@Override boolean parse(final FastDateParser parser, final Calendar calendar, final String source, final ParsePosition pos, final int maxWidth) { for (int idx = 0; idx < formatField.length(); ++idx) { final int sIdx = idx + pos.getIndex(); if (sIdx == source.length()) { pos.setErrorIndex(sIdx); return false; } if (formatField.charAt(idx) != source.charAt(sIdx)) { pos.setErrorIndex(sIdx); return false; } } pos.setIndex(formatField.length() + pos.getIndex()); return true; } }
final ParsePosition pos = new ParsePosition(0); final Calendar calendar = Calendar.getInstance(tz, lcl); calendar.setLenient(lenient); calendar.clear(); try { if (fdp.parse(str, pos, calendar) && pos.getIndex()==str.length()) { return calendar.getTime(); pos.setIndex(0); throw new ParseException("Unable to parse the date: " + str, -1);
public double parseDecimal(String input) throws ParseException{ NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.getDefault()); ParsePosition parsePosition = new ParsePosition(0); Number number = numberFormat.parse(input, parsePosition); if(parsePosition.getIndex() != input.length()){ throw new ParseException("Invalid input", parsePosition.getIndex()); } return number.doubleValue(); }