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:cfa.vo.sed.science.stacker.SedStackerFrame.java

private static boolean isNumeric(String str) {
    NumberFormat formatter = NumberFormat.getInstance();
    ParsePosition pos = new ParsePosition(0);
    formatter.parse(str, pos);/*from   w w w .  j av  a2  s .c  om*/
    return str.length() == pos.getIndex();
}

From source file:org.opendatakit.common.android.utilities.WebUtils.java

@SuppressLint("SimpleDateFormat")
private Date parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz) {
    // borrowed from apache.commons.lang.DateUtils...
    Date d = null;//from w w  w.  ja  v  a 2 s . c  o  m
    SimpleDateFormat parser = null;
    ParsePosition pos = new ParsePosition(0);
    for (int i = 0; i < parsePatterns.length; i++) {
        if (i == 0) {
            if (l == null) {
                parser = new SimpleDateFormat(parsePatterns[0]);
            } else {
                parser = new SimpleDateFormat(parsePatterns[0], l);
            }
        } else {
            parser.applyPattern(parsePatterns[i]);
        }
        parser.setTimeZone(tz); // enforce UTC for formats without timezones
        pos.setIndex(0);
        d = parser.parse(value, pos);
        if (d != null && pos.getIndex() == value.length()) {
            return d;
        }
    }
    return d;
}

From source file:org.orcid.frontend.web.controllers.FundingsController.java

/**
 * Transforms a string into a BigDecimal
 * // ww w.j av  a  2  s.c o  m
 * @param amount
 * @param locale
 * @return a BigDecimal containing the given amount
 * @throws Exception
 *             if the amount cannot be correctly parse into a BigDecimal
 * */
public BigDecimal getAmountAsBigDecimal(String amount, Locale locale) throws Exception {
    try {
        ParsePosition parsePosition = new ParsePosition(0);
        DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
        DecimalFormatSymbols symbols = numberFormat.getDecimalFormatSymbols();
        /**
         * When spaces are allowed, the grouping separator is the character
         * 160, which is a non-breaking space So, lets change it so it uses
         * the default space as a separator
         * */
        if (symbols.getGroupingSeparator() == 160) {
            symbols.setGroupingSeparator(' ');
        }
        numberFormat.setDecimalFormatSymbols(symbols);
        Number number = numberFormat.parse(amount, parsePosition);
        if (number == null || parsePosition.getIndex() != amount.length()) {
            throw new Exception();
        }
        return new BigDecimal(number.toString());
    } catch (Exception e) {
        throw e;
    }
}

From source file:com.s3d.webapps.util.time.DateUtils.java

/**
 * <p>Parses a string representing a date by trying a variety of different parsers.</p>
 * // ww  w. j a va  2  s. c o  m
 * <p>The parse will try each parse pattern in turn.
 * A parse is only deemed successful if it parses the whole of the input string.
 * If no parse patterns match, a ParseException is thrown.</p>
 * 
 * @param str  the date to parse, not null
 * @param parsePatterns  the date format patterns to use, see SimpleDateFormat, not null
 * @param lenient Specify whether or not date/time parsing is to be lenient.
 * @return the parsed date
 * @throws IllegalArgumentException if the date string or pattern array is null
 * @throws ParseException if none of the date patterns were suitable
 * @see java.util.Calender#isLenient()
 */
private static Date parseDateWithLeniency(String str, String[] parsePatterns, boolean lenient)
        throws ParseException {
    if (str == null || parsePatterns == null) {
        throw new IllegalArgumentException("Date and Patterns must not be null");
    }

    SimpleDateFormat parser = new SimpleDateFormat();
    parser.setLenient(lenient);
    ParsePosition pos = new ParsePosition(0);
    for (int i = 0; i < parsePatterns.length; i++) {

        String pattern = parsePatterns[i];

        // LANG-530 - need to make sure 'ZZ' output doesn't get passed to SimpleDateFormat
        if (parsePatterns[i].endsWith("ZZ")) {
            pattern = pattern.substring(0, pattern.length() - 1);
        }

        parser.applyPattern(pattern);
        pos.setIndex(0);

        String str2 = str;
        // LANG-530 - need to make sure 'ZZ' output doesn't hit SimpleDateFormat as it will ParseException
        if (parsePatterns[i].endsWith("ZZ")) {
            int signIdx = indexOfSignChars(str2, 0);
            while (signIdx >= 0) {
                str2 = reformatTimezone(str2, signIdx);
                signIdx = indexOfSignChars(str2, ++signIdx);
            }
        }

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

From source file:org.orcid.core.manager.impl.OrcidProfileManagerImpl.java

/**
 * Replace the funding amount string into the desired format
 * //  ww w  .jav  a2 s. c o m
 * @param updatedOrcidProfile
 *            The profile containing the new funding
 * */
private void setFundingAmountsWithTheCorrectFormat(OrcidProfile updatedOrcidProfile)
        throws IllegalArgumentException {
    FundingList fundings = updatedOrcidProfile.retrieveFundings();

    for (Funding funding : fundings.getFundings()) {
        // If the amount is not empty, update it
        if (funding.getAmount() != null && StringUtils.isNotBlank(funding.getAmount().getContent())) {
            String amount = funding.getAmount().getContent();
            Locale locale = localeManager.getLocale();
            ParsePosition parsePosition = new ParsePosition(0);
            DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
            DecimalFormatSymbols symbols = numberFormat.getDecimalFormatSymbols();
            /**
             * When spaces are allowed, the grouping separator is the
             * character 160, which is a non-breaking space So, lets change
             * it so it uses the default space as a separator
             * */
            if (symbols.getGroupingSeparator() == 160) {
                symbols.setGroupingSeparator(' ');
            }
            numberFormat.setDecimalFormatSymbols(symbols);
            Number number = numberFormat.parse(amount, parsePosition);
            String formattedAmount = number.toString();
            if (parsePosition.getIndex() != amount.length()) {
                double example = 1234567.89;
                NumberFormat numberFormatExample = NumberFormat.getNumberInstance(localeManager.getLocale());
                throw new IllegalArgumentException(
                        "The amount: " + amount + " doesn'n have the right format, it should use the format: "
                                + numberFormatExample.format(example));
            }
            funding.getAmount().setContent(formattedAmount);
        }
    }
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*from  w w w.j  a va2s  .c o  m*/
 * Parses a string representing a date by trying a variety of different
 * parsers.
 * </p>
 * 
 * <p>
 * The parse will try each parse pattern in turn. A parse is only deemed
 * successful if it parses the whole of the input string. If no parse
 * patterns match, a ParseException is thrown.
 * </p>
 * 
 * @param str
 *            the date to parse, not null
 * @param parsePatterns
 *            the date format patterns to use, see SimpleDateFormat, not
 *            null
 * @param lenient
 *            Specify whether or not date/time parsing is to be lenient.
 * @return the parsed date
 * @throws IllegalArgumentException
 *             if the date string or pattern array is null
 * @throws ParseException
 *             if none of the date patterns were suitable
 * @see java.util.Calender#isLenient()
 */
private static Date parseDateWithLeniency(String str, String[] parsePatterns, boolean lenient)
        throws ParseException {
    if (str == null || parsePatterns == null) {
        throw new IllegalArgumentException("Date and Patterns must not be null");
    }

    SimpleDateFormat parser = new SimpleDateFormat();
    parser.setLenient(lenient);
    ParsePosition pos = new ParsePosition(0);
    for (int i = 0; i < parsePatterns.length; i++) {

        String pattern = parsePatterns[i];

        // LANG-530 - need to make sure 'ZZ' output doesn't get passed to
        // SimpleDateFormat
        if (parsePatterns[i].endsWith("ZZ")) {
            pattern = pattern.substring(0, pattern.length() - 1);
        }

        parser.applyPattern(pattern);
        pos.setIndex(0);

        String str2 = str;
        // LANG-530 - need to make sure 'ZZ' output doesn't hit
        // SimpleDateFormat as it will ParseException
        if (parsePatterns[i].endsWith("ZZ")) {
            str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2");
        }

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