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:org.jbpm.formModeler.service.bb.mvc.components.FactoryURL.java

public static FactoryURL getURL(String value) throws ParseException {
    ParsePosition pPos = new ParsePosition(0);
    Object[] o = msgf.parse(value, pPos);
    if (o == null)
        throw new ParseException("Cannot parse " + value + ". Error at position " + pPos.getErrorIndex(),
                pPos.getErrorIndex());/*from   ww w  .  j  a v a  2 s .co m*/
    String componentName = StringEscapeUtils.unescapeHtml4((String) o[0]);
    String propertyName = StringEscapeUtils.unescapeHtml4((String) o[1]);
    return new FactoryURL(componentName, propertyName);
}

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

public static Date fromString(String value) {
    Date newDate = null;//from   ww  w.java  2s  .c  o m
    if (StringUtils.isNotBlank(value)) {
        SimpleDateFormat sdf = new SimpleDateFormat(OMP_DATE_FORMAT);
        newDate = sdf.parse(value, new ParsePosition(0));
    }
    return newDate;
}

From source file:org.mule.util.DateUtils.java

public static Date getDateFromString(String date, String format) {
    // The date must always be in the format of TIME_STAMP_FORMAT
    // i.e. JAN 29 2001 22:50:40 GMT
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    ParsePosition pos = new ParsePosition(0);

    // Parse the string back into a Time Stamp.
    return formatter.parse(date, pos);
}

From source file:com.mgmtp.perfload.core.client.util.PlaceholderUtils.java

/**
 * Replaces all placeholders in the specified string using the specified map. If a placeholder
 * value cannot be found in the map, the placeholder is left as is.
 * //from   ww w  .  j  av  a2 s.co m
 * @param input
 *            the string to parse
 * @param replacements
 *            a map containing replacement values for placeholders
 * @return the string with all placeholders resolved
 */
public static String resolvePlaceholders(final String input, final Map<String, String> replacements) {
    if (isBlank(input)) {
        return input;
    }

    final int len = input.length();
    ParsePosition pos = new ParsePosition(0);
    String result = resolveNextPlaceholder(input, pos, replacements);
    if (result != null && pos.getIndex() >= len) {
        // we are done if there was no next placeholder and the
        // parse position is already at the end of the string
        return result;
    }
    StringBuilder sb = new StringBuilder(len * 2);
    if (result == null) {
        // Add the character if no placeholder is at the current position
        // and increment the position
        sb.append(input.charAt(pos.getIndex()));
        pos.setIndex(pos.getIndex() + 1);
    } else {
        sb.append(result);
    }

    // loop as long as the parse position is less than the input string's length
    while (pos.getIndex() < len) {
        result = resolveNextPlaceholder(input, pos, replacements);
        if (result == null) {
            // Add the character if no placeholder is at the current position
            // and increment the position
            sb.append(input.charAt(pos.getIndex()));
            pos.setIndex(pos.getIndex() + 1);
        } else {
            sb.append(result);
        }
    }
    return sb.toString();
}

From source file:Main.java

/** Returns a Date object from a String. */
public static Date parseDate(String dateStr) {
    String formatString = "";

    if (dateStr.length() == 16)
        dateStr = dateStr.substring(0, 14);
    if (dateStr.length() == 15)
        formatString = "yyyyMMdd'T'hhmmss";
    if (dateStr.length() == 8)
        formatString = "yyyyMMdd";

    SimpleDateFormat formatter = new SimpleDateFormat(formatString);
    ParsePosition pos = new ParsePosition(0);

    return formatter.parse(dateStr, pos);
}

From source file:be.wegenenverkeer.common.resteasy.json.DateConverter.java

@Override
public Date fromString(String str) {
    Date date = null;// w w w  . j a va2 s. c  o m

    if (StringUtils.isNotBlank(str)) {
        try {
            return iso8601AndOthers.parse(str, new ParsePosition(0));
        } catch (IllegalArgumentException iae) {
            // ignore, try next format
            date = null; // dummy
        }
    }
    return date; // empty string
}

From source file:org.jboss.dashboard.ui.formatters.FactoryURL.java

public FactoryURL(String value) throws ParseException {
    ParsePosition pPos = new ParsePosition(0);
    Object[] o = msgf.parse(value, pPos);
    if (o == null)
        throw new ParseException("Cannot parse " + value + ". Error at position " + pPos.getErrorIndex(),
                pPos.getErrorIndex());//from   www  . j  av  a2 s  .c  o m

    beanName = StringEscapeUtils.UNESCAPE_HTML4.translate((String) o[0]);
    fieldName = StringEscapeUtils.UNESCAPE_HTML4.translate((String) o[1]);
}

From source file:org.totschnig.myexpenses.Utils.java

/**
 * <a href="http://www.ibm.com/developerworks/java/library/j-numberformat/">http://www.ibm.com/developerworks/java/library/j-numberformat/</a>
 * @param strFloat parsed as float with the number format defined in the locale
 * @return the float retrieved from the string or null if parse did not succeed
 *///ww w . j a v a2s  . c  om
public static Float validateNumber(String strFloat) {
    ParsePosition pp;
    NumberFormat nfDLocal = NumberFormat.getNumberInstance();
    nfDLocal.setGroupingUsed(false);
    pp = new ParsePosition(0);
    pp.setIndex(0);
    Number n = nfDLocal.parse(strFloat, pp);
    if (strFloat.length() != pp.getIndex() || n == null) {
        return null;
    } else {
        return n.floatValue();
    }
}

From source file:com.taobao.datax.common.util.StrUtils.java

License:asdf

/**
 * Convert the string date to a particular format.
 * //from   w w  w.ja  v a  2 s. c  om
 * @param   dateString
 *          The String to operate on.
 *            
 * @param   srcFormat
 *          The date Format contained in the String.
 *            
 * @param   destFormat
 *          The format to be converted to.
 *            
 * @return 
 *          Formatted Date String if successful, null otherwise.
 * 
 */
public static String changeDateFormat(String dateString, String srcFormat, String destFormat) {
    if (srcFormat == null || destFormat == null || srcFormat.isEmpty() || destFormat.isEmpty()) {
        return "";
    }

    // Set the Date Format for the vaules in the column specified
    SimpleDateFormat dateFormat = new SimpleDateFormat(srcFormat);

    if (dateString == null || dateString.isEmpty()) {
        return "";
    }

    // Get date as per the format
    java.util.Date dateVal = dateFormat.parse(dateString, new ParsePosition(0));

    dateFormat = new SimpleDateFormat(destFormat);

    return dateFormat.format(dateVal);
}

From source file:org.webguitoolkit.ui.controls.util.validation.PositiveNumberValidator.java

public void validate(String object) throws ValidationException {
    try {// w w w  .  j a v a2 s.com
        // no mandatory field: empty means value zero.
        if (StringUtils.isEmpty(object)) {
            return;
        } else {
            DecimalFormat formatter;
            formatter = new DecimalFormat(pattern, new DecimalFormatSymbols(TextService.getLocale()));

            ParsePosition pos = new ParsePosition(0);
            Number num = formatter.parse(object, pos);

            if (num == null)
                throw new ValidationException(TextService.getString(
                        "converter.PositiveNumberValidator.message.conversion@Cannot convert into number."));
            if (pos.getIndex() < object.length()) {
                throw new ValidationException(TextService.getString(
                        "converter.PositiveNumberValidator.message.conversion@Cannot convert into number."));
            }

            Double f = new Double(num.doubleValue());
            if (f.doubleValue() < 0) {
                throw new ValidationException(TextService.getString(
                        "validator.PositiveNumberValidator.message@The entered number must be positive."));
            }
        }
    } catch (NumberFormatException e) {
        throw new ValidationException(TextService
                .getString("validator.PositiveNumberValidator.message.conversion@Cannot convert into number."));
    }
}