Example usage for java.text ParsePosition getIndex

List of usage examples for java.text ParsePosition getIndex

Introduction

In this page you can find the example usage for java.text ParsePosition getIndex.

Prototype

public int getIndex() 

Source Link

Document

Retrieve the current parse position.

Usage

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a float primitive.
 * /* w ww  . j ava2  s. com*/
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Float value.
 */
public static Float formatFloat(String value, Locale locale) {
    Float result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getInstance(locale);
        } else {
            formatter = NumberFormat.getInstance(Locale.getDefault());
        }
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= (Float.MAX_VALUE * -1) && num.doubleValue() <= Float.MAX_VALUE) {
                result = new Float(num.floatValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a double primitive.
 * //from   w  w w .  j a  v a 2s  . c om
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Double value.
 */
public static Double formatDouble(String value, Locale locale) {
    Double result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getInstance(locale);
        } else {
            formatter = NumberFormat.getInstance(Locale.getDefault());
        }
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= (Double.MAX_VALUE * -1) && num.doubleValue() <= Double.MAX_VALUE) {
                result = new Double(num.doubleValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a byte primitive.
 * //  www  . j ava 2 s  .co m
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Byte value.
 */
public static Byte formatByte(String value, Locale locale) {
    Byte result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) {
                result = new Byte(num.byteValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a short primitive.
 * /*from  w  w w .  j  av  a  2 s .c om*/
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)vv
 * @return the converted Short value.
 */
public static Short formatShort(String value, Locale locale) {
    Short result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Short.MIN_VALUE && num.doubleValue() <= Short.MAX_VALUE) {
                result = new Short(num.shortValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to an int primitive.
 * //from w  w  w.j a  v  a  2  s  .com
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Integer value.
 */
public static Integer formatInt(String value, Locale locale) {
    Integer result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) {
                result = new Integer(num.intValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a long primitive.
 * /*from   w w w . j a  v  a 2  s.  c om*/
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Long value.
 */
public static Long formatLong(String value, Locale locale) {
    Long result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getNumberInstance(locale);
        } else {
            formatter = NumberFormat.getNumberInstance(Locale.getDefault());
        }
        formatter.setParseIntegerOnly(true);
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= Long.MIN_VALUE && num.doubleValue() <= Long.MAX_VALUE) {
                result = new Long(num.longValue());
            }
        }
    }

    return result;
}

From source file:org.opendatakit.common.utils.WebUtils.java

/**
 * Parse a string into a datetime value. Tries the common Http formats, the
 * iso8601 format (used by Javarosa), the default formatting from
 * Date.toString(), and a time-only format.
 *
 * @param value/*from   w w  w.  j a  v  a2s .  c  om*/
 * @return
 */
public static final Date parseDate(String value) {
    if (value == null || value.length() == 0)
        return null;

    String[] iso8601Pattern = new String[] { PATTERN_ISO8601 };

    String[] localizedParsePatterns = new String[] {
            // try the common HTTP date formats that have time zones
            PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_DATE_TOSTRING };

    String[] localizedNoTzParsePatterns = new String[] {
            // ones without timezones... (will assume UTC)
            PATTERN_ASCTIME };

    String[] tzParsePatterns = new String[] { PATTERN_ISO8601, PATTERN_ISO8601_DATE, PATTERN_ISO8601_TIME };

    String[] noTzParsePatterns = new String[] {
            // ones without timezones... (will assume UTC)
            PATTERN_ISO8601_WITHOUT_ZONE, PATTERN_NO_DATE_TIME_ONLY, PATTERN_YYYY_MM_DD_DATE_ONLY_NO_TIME_DASH,
            PATTERN_GOOGLE_DOCS };

    Date d = null;
    // iso8601 parsing is sometimes off-by-one when JR does it...
    d = parseDateSubset(value, iso8601Pattern, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try to parse with the JavaRosa parsers
    d = DateUtils.parseDateTime(value);
    if (d != null)
        return d;
    d = DateUtils.parseDate(value);
    if (d != null)
        return d;
    d = DateUtils.parseTime(value);
    if (d != null)
        return d;
    // try localized and english text parsers (for Web headers and interactive
    // filter spec.)
    d = parseDateSubset(value, localizedParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedNoTzParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, localizedNoTzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try other common patterns that might not quite match JavaRosa parsers
    d = parseDateSubset(value, tzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    d = parseDateSubset(value, noTzParsePatterns, null, TimeZone.getTimeZone("GMT"));
    if (d != null)
        return d;
    // try the locale- and timezone- specific parsers
    {
        DateFormat formatter = DateFormat.getDateTimeInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    {
        DateFormat formatter = DateFormat.getDateInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    {
        DateFormat formatter = DateFormat.getTimeInstance();
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    throw new IllegalArgumentException("Unable to parse the date: " + value);
}

From source file:org.pentaho.reporting.libraries.formula.typing.DefaultTypeRegistry.java

private static Date parse(final DateFormat format, final String source) {
    final ParsePosition parsePosition = new ParsePosition(0);
    final Date result = format.parse(source, parsePosition);
    if (parsePosition.getIndex() == 0 || parsePosition.getIndex() != source.length()) {
        return null;
    }/* w  w  w  .  j av a 2 s  . c  om*/
    return result;
}

From source file:org.pentaho.reporting.libraries.formula.typing.DefaultTypeRegistry.java

private static Number parse(final NumberFormat format, final String source) {
    final ParsePosition parsePosition = new ParsePosition(0);
    final Number result = format.parse(source, parsePosition);
    if (parsePosition.getIndex() == 0 || parsePosition.getIndex() != source.length()) {
        return null;
    }//from  ww  w .  ja  va 2s  .  c o m
    return result;
}

From source file:gov.nih.nci.cabig.caaers.utils.DateUtils.java

public static Date parseDate(String dateStr, String... parsePatterns) throws ParseException {

    if (dateStr == null || parsePatterns == null) {
        throw new IllegalArgumentException("Date and Patterns must not be null");
    }/*from  ww w  .  j  ava 2  s  .c  o  m*/

    String strDate = dateStr;
    //do year correction. (partial year >=50 will be 1999 and <50 will be 2000)
    String[] parts = StringUtils.split(dateStr, '/');
    int len = parts.length;

    if (len != 3 || parts[0].length() > 2 || parts[1].length() > 2)
        throw new ParseException("Unable to parse the date " + strDate, -1);

    String yStr = parts[2];

    if (!(yStr.length() == 4 || yStr.length() == 2 || yStr.length() == 10))
        throw new ParseException("Unable to parse the date " + strDate, -1);
    if (yStr.length() == 2 && StringUtils.isNumeric(yStr)) {

        if (Integer.parseInt(yStr) < 50)
            yStr = "20" + yStr;
        else
            yStr = "19" + yStr;

        parts[2] = yStr;
        strDate = StringUtils.join(parts, '/');
    }

    //BJ: date formats are not thread save, so we need to create one each time.
    SimpleDateFormat parser = null;
    ParsePosition pos = new ParsePosition(0);
    for (int i = 0; i < parsePatterns.length; i++) {
        if (i == 0) {
            parser = new SimpleDateFormat(parsePatterns[0]);
        } else {
            parser.applyPattern(parsePatterns[i]);
        }
        pos.setIndex(0);

        Date date = parser.parse(strDate, pos);
        if (date != null && pos.getIndex() == strDate.length()) {
            return date;
        }
    }
    throw new ParseException("Unable to parse the date: " + strDate, -1);
}