Example usage for java.text ParsePosition ParsePosition

List of usage examples for java.text ParsePosition ParsePosition

Introduction

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

Prototype

public ParsePosition(int index) 

Source Link

Document

Create a new ParsePosition with the given initial index.

Usage

From source file:de.unioninvestment.eai.portal.portlet.crud.scripting.domain.container.rest.ValueConverter.java

private Object convertStringToValue(Class<?> targetClass, String format, Locale locale, String value) {

    try {//  w  ww. jav  a2 s . c o  m
        Locale effectiveLocale = locale != null ? locale : Locale.US;
        NumberFormat nf = new NumberFormatter(format).getNumberFormat(effectiveLocale);
        Number number = null;
        if (targetClass == BigDecimal.class) {
            ((DecimalFormat) nf).setParseBigDecimal(true);
            number = nf.parse(value, new ParsePosition(0));
        } else {
            number = nf.parse(value);
        }
        return convertNumberToNumber(targetClass, number);

    } catch (ParseException e) {
        // continue to exception
    }

    throw new IllegalArgumentException("Cannot convert to " + targetClass.getSimpleName() + ": " + value);
}

From source file:com.anrisoftware.fractions.format.FractionFormat.java

/**
 * @see #parse(String, ParsePosition)/* w  w  w. jav  a 2 s .  c  o  m*/
 */
public ContinuedFraction parse(String source) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    ContinuedFraction result = parse(source, pos);
    if (pos.getIndex() == 0) {
        throw log.errorParse(source, pos);
    }
    return result;
}

From source file:org.apache.falcon.util.DateUtil.java

/**
 * Parses a datetime in ISO8601 format in the  process timezone.
 *
 * @param s string with the datetime to parse.
 * @return the corresponding {@link java.util.Date} instance for the parsed date.
 * @throws java.text.ParseException thrown if the given string was
 * not an ISO8601 value for the  process timezone.
 *///from www  .j a va 2  s .  c o  m
public static Date parseDateFalconTZ(String s) throws ParseException {
    s = s.trim();
    ParsePosition pos = new ParsePosition(0);
    Date d = getISO8601DateFormat(activeTimeZone, activeTimeMask).parse(s, pos);
    if (d == null) {
        throw new ParseException("Could not parse [" + s + "] using [" + activeTimeMask + "] mask",
                pos.getErrorIndex());
    }
    if (s.length() > pos.getIndex()) {
        throw new ParseException("Correct datetime string is followed by invalid characters: " + s,
                pos.getIndex());
    }
    return d;
}

From source file:storybook.toolkit.DateUtil.java

public static Date stringToDate(String str) {
    Date d = null;//from ww  w.  j a  v a 2s .  c o  m
    try {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        d = formatter.parse(str, pos);
    } catch (RuntimeException e) {
    }
    return d;
}

From source file:edu.usu.sdl.openstorefront.common.util.Convert.java

/**
 * Attempts to convert several different formats
 *
 * MM/dd/yyyy HH:mm:ss z yyyy-mm-dd HH:mm:ss z yyyy-MM-dd'T'HH:mm:ss.SSS'Z
 * MM/dd/yyyy yyyy-mm-dd date standard milliseconds
 *
 * @param dateString//from   w  ww  .  j a v a 2  s  . c o m
 * @return date
 */
public static Date toDate(String dateString) {
    Date dateConverted = null;

    if (StringUtils.isNotBlank(dateString)) {

        try {
            if (StringUtils.isNumeric(dateString)) {
                dateConverted = new Date(Long.parseLong(dateString));
            } else {

                String formats[] = { "MM/dd/yyyy HH:mm:ss z ", "yyyy-mm-dd HH:mm:ss z ",
                        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z", "MM/dd/yyyy", "yyyy-mm-dd ", };

                for (String format : formats) {
                    SimpleDateFormat sdf = new SimpleDateFormat(format);
                    dateConverted = sdf.parse(dateString, new ParsePosition(0));
                    if (dateConverted != null) {
                        break;
                    }
                }
            }

        } catch (Exception e) {
            dateConverted = null;
        }
    }

    return dateConverted;
}

From source file:com.anrisoftware.globalpom.format.measurement.MeasureFormat.java

/**
 * Parses the specified string to physical measurement.
 * <p>// w w w  . j  av a2  s .  co  m
 * The format follows the pattern:
 * 
 * <pre>
 * &lt;value&gt;[(&lt;uncertainty&gt;);&lt;significant&gt;;&lt;decimal&gt;;&lt;unit&gt;;]
 * </pre>
 * 
 * <p>
 * <h2>Examples</h2>
 * <p>
 * <ul>
 * <li>exact value: {@code 0.0123;m/s;}
 * <li>uncertain value: {@code 5.0(0.2);1;1;m/s;}
 * </ul>
 * 
 * @return the parsed {@link Measure}.
 * 
 * @param <UnitType>
 *            the {@link Quantity} of the unit.
 * 
 * @throws ParseException
 *             if the string cannot be parsed to a value.
 * 
 * @since 1.11
 */
public <UnitType extends Quantity> Measure<UnitType> parse(String source) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Measure<?> result = parse(source, pos);
    if (pos.getIndex() == 0) {
        throw log.errorParseValue(source, pos);
    }
    return toUnitType(result);
}

From source file:com.anrisoftware.globalpom.format.point.PointFormat.java

/**
 * Parses the specified string to a point.
 * <p>/*from   w w  w. j  a  va 2  s .  c  o  m*/
 * The parser expects the patterns:
 * 
 * <ul>
 * <li><code>(x, y)</code></li>
 * <li><code>(x,y)</code></li>
 * <li><code>x, y</code></li>
 * <li><code>x,y</code></li>
 * </ul>
 * 
 * @param point
 *            the {@link Point2D} that should be parsed. If {@code null} a
 *            {@link Point2D#Double} is used.
 * 
 * @return the parsed {@link Point2D}.
 * 
 * @throws ParseException
 *             if the string cannot be parsed to a point.
 * 
 * @see Point
 * @see Point2D#Double
 * @see Point2D#Float
 */
public Point2D parse(String source, Point2D point) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    Point2D result = parse(source, pos, point);
    if (pos.getIndex() == 0) {
        throw log.errorParsePoint(source, pos);
    }
    return result;
}

From source file:jp.co.ctc_g.jfw.core.util.Dates.java

/**
 * ??????????/*from ww  w. j  a  v a2  s .  c om*/
 * {@link Date}????
 * ?<code>null</code>???????????
 * ???<code>null</code>????
 * ?{@link SimpleDateFormat}????
 * ??????????
 * ?????????null????
 * @param source ??
 * @param pattern 
 * @return ????{@link Date}
 */
public static Date makeFrom(String source, String pattern) {
    if (Strings.isEmpty(source) || Strings.isEmpty(pattern))
        return null;
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    sdf.setLenient(true);
    ParsePosition pos = new ParsePosition(0);
    Date date = sdf.parse(source, pos);
    if (pos.getErrorIndex() != -1 && L.isDebugEnabled()) {
        Map<String, Object> replace = new HashMap<String, Object>(3);
        replace.put("pattern", pattern);
        replace.put("index", pos.getErrorIndex() + 1);
        replace.put("target", source);
        L.debug(Strings.substitute(R.getString("D-UTIL#0002"), replace));
    }
    return date;
}

From source file:com.anrisoftware.sscontrol.ldap.dbindex.DbIndexFormat.java

/**
 * @see #parse(String, ParsePosition)/*from   w w  w.  j a  v a 2 s .com*/
 */
public DbIndex parse(String source) throws ParseException {
    ParsePosition pos = new ParsePosition(0);
    DbIndex result = parse(source, pos);
    if (pos.getIndex() == 0) {
        throw log.errorParseIndex(source, pos);
    }
    return result;
}

From source file:org.jbpm.formModeler.core.processing.fieldHandlers.NumericFieldHandler.java

public Object getTheValue(Field field, String[] paramValue, String desiredClassName) throws Exception {
    if (paramValue == null || paramValue.length == 0)
        return null;

    if (desiredClassName.equals("byte")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Byte((byte) 0);
        else/*from  www.  j a v  a 2 s  .  co  m*/
            return Byte.decode(paramValue[0]);
    } else if (desiredClassName.equals("short")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Short((short) 0);
        else
            return Short.decode(paramValue[0]);
    } else if (desiredClassName.equals("int")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Integer(0);
        else
            return Integer.decode(paramValue[0]);
    } else if (desiredClassName.equals("long")) {
        if (StringUtils.isEmpty(paramValue[0]))
            return new Long(0L);
        else
            return Long.decode(paramValue[0]);
    } else if (desiredClassName.equals(Byte.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Byte.decode(paramValue[0]);
    } else if (desiredClassName.equals(Short.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Short.decode(paramValue[0]);

    } else if (desiredClassName.equals(Integer.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Integer.decode(paramValue[0]);

    } else if (desiredClassName.equals(Long.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return Long.decode(paramValue[0]);

    } else if (desiredClassName.equals(Double.class.getName()) || desiredClassName.equals("double")
            || desiredClassName.equals(Float.class.getName()) || desiredClassName.equals("float")
            || desiredClassName.equals(BigDecimal.class.getName())) {

        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();

        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale(LocaleManager.currentLang()));
        if (desiredClassName.equals(BigDecimal.class.getName()))
            df.setParseBigDecimal(true);
        String pattern = getFieldPattern(field);
        if (pattern != null && !"".equals(pattern)) {
            df.applyPattern(pattern);
        } else {
            df.applyPattern("###.##");
        }
        ParsePosition pp = new ParsePosition(0);
        Number num = df.parse(paramValue[0], pp);
        if (paramValue[0].length() != pp.getIndex() || num == null) {
            log.debug("Error on parsing value");
            throw new ParseException("Error parsing value", pp.getIndex());
        }

        if (desiredClassName.equals(BigDecimal.class.getName())) {
            return num;
        } else if (desiredClassName.equals(Float.class.getName()) || desiredClassName.equals("float")) {
            return new Float(num.floatValue());
        } else if (desiredClassName.equals(Double.class.getName()) || desiredClassName.equals("double")) {
            return new Double(num.doubleValue());
        }
    } else if (desiredClassName.equals(BigInteger.class.getName())) {
        if (StringUtils.isEmpty(paramValue[0]))
            throw new EmptyNumberException();
        return new BigInteger(paramValue[0]);

    }
    throw new IllegalArgumentException("Invalid class for NumericFieldHandler: " + desiredClassName);
}