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; }
public static Date getDate(String sDate, String dateFormat) { SimpleDateFormat fmt = new SimpleDateFormat(dateFormat); ParsePosition pos = new ParsePosition(0); return fmt.parse(sDate, pos); }
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 position = new ParsePosition(0); Date result = STANDARD_DATE_FORMAT.get().parse(value, position); if (position.getIndex() == value.length()) { DateFormat format = BROWSER_COMPATIBLE_DATE_FORMATS[i]; if (format == null) { format = new SimpleDateFormat(BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS[i], Locale.US); if (position.getIndex() != 0) {
pp = new ParsePosition(0); if (BigDecimal.class.isAssignableFrom(populateValueRequest.getReturnType())) { format.setParseBigDecimal(true); format.parse(populateValueRequest.getRequestedValue(), pp); if (pp.getIndex() != populateValueRequest.getRequestedValue().length()) { return new PropertyValidationResult(false, "Field must be a valid decimal"); pp = new ParsePosition(0); try { if (Double.class.isAssignableFrom(populateValueRequest.getReturnType())) { format.setParseBigDecimal(false); if (pp.getIndex() != populateValueRequest.getRequestedValue().length()) { return new PropertyValidationResult(false, "Field must be a valid number"); case DATE: try { populateValueRequest.getDataFormatProvider().getSimpleDateFormatter().parse(populateValueRequest.getRequestedValue()); } catch (ParseException e) { return new PropertyValidationResult(false, "Field must be a date of the format: " + populateValueRequest.getDataFormatProvider().getSimpleDateFormatter().toPattern());
protected synchronized Date convertStringToDate( String string ) throws KettleValueException { string = Const.trimToType( string, getTrimType() ); // see if trimming needs // to be performed before // conversion if ( Utils.isEmpty( string ) ) { return null; } try { ParsePosition pp = new ParsePosition( 0 ); Date result = getDateFormat( TYPE_DATE ).parse( string, pp ); if ( pp.getErrorIndex() >= 0 ) { // error happen throw new ParseException( string, pp.getErrorIndex() ); } // some chars can be after pp.getIndex(). That means, not full value was parsed. For example, for value // "25-03-1918 11:54" and format "dd-MM-yyyy", value will be "25-03-1918 00:00" without any exception. // If there are only spaces after pp.getIndex() - that means full values was parsed return result; } catch ( ParseException e ) { String dateFormat = ( getDateFormat() != null ) ? getDateFormat().toPattern() : "null"; throw new KettleValueException( toString() + " : couldn't convert string [" + string + "] to a date using format [" + dateFormat + "] on offset location " + e.getErrorOffset(), e ); } }
void test(SimpleDateFormat sdf, String date) throws Exception { DateFormat df = sdf; if (date == null) System.out.println("oops"); date = null; Date d1 = df.parse(date); ParsePosition p = new ParsePosition(0); Date d2 = df.parse(date, p); Date d3 = sdf.parse(date); Date d4 = sdf.parse(date, p); }
/** {@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; }
@Override public Date parse(final String source) throws ParseException { final ParsePosition pp = new ParsePosition(0); final Date date= parse(source, pp); if (date == null) { // Add a note re supported date range if (locale.equals(JAPANESE_IMPERIAL)) { throw new ParseException( "(The " +locale + " locale does not support dates before 1868 AD)\n" + "Unparseable date: \""+source, pp.getErrorIndex()); } throw new ParseException("Unparseable date: "+source, pp.getErrorIndex()); } return date; }
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(); throw new ParseException("Unable to parse the date: " + str, -1);
public Date parse(final String source) throws ParseException { final Date date = parse(source, new ParsePosition(0)); if (date == null) { // Add a note re supported date range if (locale.equals(JAPANESE_IMPERIAL)) { throw new ParseException( "(The " + locale + " locale does not support dates before 1868 AD)\n" + "Unparseable date: \"" + source + "\" does not match " + parsePattern.pattern(), 0); } throw new ParseException("Unparseable date: \"" + source + "\" does not match " + parsePattern.pattern(), 0); } return date; }
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(); }
private int determineDepth( String depthString ) throws KettleException { DecimalFormat df = new DecimalFormat( "0" ); ParsePosition pp = new ParsePosition( 0 ); df.setParseIntegerOnly( true ); try { Number n = df.parse( depthString, pp ); if ( n == null ) { return 1; // default } if ( pp.getErrorIndex() == 0 ) { throw new KettleException( "Unable to convert stored depth '" + depthString + "' to depth at position " + pp.getErrorIndex() ); } return n.intValue(); } catch ( Exception e ) { throw new KettleException( "Unable to convert stored depth '" + depthString + "' to depth", e ); } }
final String trimmedDate = semicolonIndex >= 0 ? date.substring(0, semicolonIndex) : date; ParsePosition pp = new ParsePosition(0); SimpleDateFormat dateFormat = RFC1123_PATTERN_FORMAT.get(); dateFormat.setTimeZone(GMT_ZONE); Date val = dateFormat.parse(trimmedDate, pp); if (val != null && pp.getIndex() == trimmedDate.length()) { return val; pp = new ParsePosition(0); dateFormat = new SimpleDateFormat(RFC1036_PATTERN, LOCALE_US); dateFormat.setTimeZone(GMT_ZONE); val = dateFormat.parse(trimmedDate, pp); if (val != null && pp.getIndex() == trimmedDate.length()) { return val; pp = new ParsePosition(0); dateFormat = new SimpleDateFormat(ASCITIME_PATTERN, LOCALE_US); dateFormat.setTimeZone(GMT_ZONE); val = dateFormat.parse(trimmedDate, pp); if (val != null && pp.getIndex() == trimmedDate.length()) { return val; pp = new ParsePosition(0); dateFormat = new SimpleDateFormat(OLD_COOKIE_PATTERN, LOCALE_US); dateFormat.setTimeZone(GMT_ZONE); val = dateFormat.parse(trimmedDate, pp);
boolean isLegalDate(String s) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); sdf.setLenient(false); return sdf.parse(s, new ParsePosition(0)) != null; }
ParsePosition position = new ParsePosition(0); Date result = STANDARD_DATE_FORMAT.get().parse(value, position); if (position.getIndex() == value.length()) { DateFormat format = BROWSER_COMPATIBLE_DATE_FORMATS[i]; if (format == null) { format = new SimpleDateFormat(BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS[i], Locale.US); if (position.getIndex() != 0) {
@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; }
final SimpleDateFormat dateParser = DateFormatHolder.formatFor(dateFormat); dateParser.set2DigitYearStart(localStartDate); final ParsePosition pos = new ParsePosition(0); final Date result = dateParser.parse(v, pos); if (pos.getIndex() != 0) { return result;
/** {@inheritDoc} */ @Override public Vector2D parse(final String source) throws MathParseException { ParsePosition parsePosition = new ParsePosition(0); Vector2D result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Vector2D.class); } return result; }