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:us.mn.state.health.lims.common.util.DateUtil.java

public static synchronized int decodeTime(String s) throws Exception {
    SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
    // System.out.println("Passed in this time " +s);
    TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
    f.setTimeZone(utcTimeZone);//from  www. j a v a  2s  . c o  m
    f.setLenient(false);
    ParsePosition p = new ParsePosition(0);
    Date d = f.parse(s, p);
    if (d == null || !StringUtil.isRestOfStringBlank(s, p.getIndex())) {
        throw new Exception("Invalid time value (hh:mm:ss): \"" + s + "\".");
    }
    return (int) d.getTime();
}

From source file:org.jumpmind.util.FormatUtils.java

public static Date parseDate(String str, String[] parsePatterns, TimeZone timeZone) {
    if (str == null || parsePatterns == null) {
        throw new IllegalArgumentException("Date and Patterns must not be null");
    }// ww  w.  ja  v  a2  s.co m

    SimpleDateFormat parser = null;
    ParsePosition pos = new ParsePosition(0);
    for (int i = 0; i < parsePatterns.length; i++) {
        if (i == 0) {
            parser = new SimpleDateFormat(parsePatterns[0]);
            if (timeZone != null) {
                parser.setTimeZone(timeZone);
            }
        } else {
            parser.applyPattern(parsePatterns[i]);
        }
        pos.setIndex(0);
        Date date = parser.parse(str, pos);
        if (date != null && pos.getIndex() == str.length()) {
            return date;
        }
    }

    try {
        Date date = new Date(Long.parseLong(str));
        return date;
    } catch (NumberFormatException e) {

    }

    throw new ParseException("Unable to parse the date: " + str);
}

From source file:adalid.commons.util.TimeUtils.java

public static java.util.Date parse(String pdq) {
    if (StringUtils.isBlank(pdq)) {
        return null;
    }//from w  w w . j  a v  a 2  s . c o m
    String string = pdq.trim();
    ParsePosition position = new ParsePosition(0);
    java.util.Date util = timestampFormatter().parse(string, position);
    int i = position.getIndex();
    int l = string.length();
    if (util != null && i == l) {
        return new Timestamp(util.getTime());
    }
    position.setIndex(0);
    util = dateFormatter().parse(string, position);
    i = position.getIndex();
    if (util != null) {
        if (i == l) {
            return new Date(util.getTime());
        }
        java.util.Date time = timeFormatter().parse(string, position);
        i = position.getIndex();
        if (time != null && i == l) {
            return merge(util, time);
        }
    }
    position.setIndex(0);
    util = timeFormatter().parse(string, position);
    i = position.getIndex();
    if (util != null && i == l) {
        return new Time(util.getTime());
    }
    return null;
}

From source file:com.glaf.core.util.DateUtils.java

public static Date parseDate(String str, String[] parsePatterns) {
    if (str == null || parsePatterns == null) {
        throw new IllegalArgumentException("Date and Patterns must not be null");
    }/*w ww  .java2 s  . c  om*/
    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(str, pos);
        if (date != null && pos.getIndex() == str.length()) {
            return date;
        }
    }
    throw new RuntimeException("Unable to parse the date: " + str);
}

From source file:com.almalence.util.Util.java

public static boolean isNumeric(String str) {
    NumberFormat formatter = NumberFormat.getInstance();
    ParsePosition pos = new ParsePosition(0);
    formatter.parse(str, pos);/*from w  w  w .j a v  a2s  . c o  m*/
    return str.length() == pos.getIndex();
}

From source file:org.opendatakit.briefcase.util.WebUtils.java

private static final Date parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz) {
    // borrowed from apache.commons.lang.DateUtils...
    Date d = null;/* ww w  .  j av a 2s  .  c  om*/
    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.osaf.cosmo.eim.schema.text.DurationFormat.java

public Object parseObject(String source, ParsePosition pos) {
    if (pos.getIndex() > 0)
        return null;

    if (!PATTERN.matcher(source).matches()) {
        parseException = new ParseException("Invalid duration " + source, 0);
        pos.setErrorIndex(0);// w  w w .ja  v a2  s.c om
        return null;
    }

    Dur dur = new Dur(source);

    pos.setIndex(source.length());

    return dur;
}

From source file:nz.co.senanque.vaadinsupport.formatting.FormatterBigDecimal.java

public Object parse(String formattedValue) throws Exception {
    ParsePosition parsePosition = new ParsePosition(0);
    Object o = nf.parseObject(formattedValue);
    if (parsePosition.getIndex() != formattedValue.length()) {
        throw new RuntimeException("Invalid number " + formattedValue);
    }/*w  w w.j  a  va2  s . c  om*/
    return o;
}

From source file:com.anrisoftware.globalpom.format.locale.LocaleFormat.java

/**
 * @see #parse(String)/*from   w w w  .ja  v a  2 s  .  c  om*/
 * 
 * @param pos
 *            the index {@link ParsePosition} position from where to start
 *            parsing.
 */
public Locale parse(String source, ParsePosition pos) {
    source = source.substring(pos.getIndex());
    try {
        Locale address = parseValue(source, pos);
        pos.setErrorIndex(-1);
        pos.setIndex(source.length() + 1);
        return address;
    } catch (ParseException e) {
        pos.setIndex(0);
        pos.setErrorIndex(0);
        return null;
    }
}

From source file:nz.co.senanque.vaadinsupport.formatting.FormatterDouble.java

public Object parse(String formattedValue) throws Exception {
    ParsePosition parsePosition = new ParsePosition(0);
    Object o = nf.parseObject(formattedValue, parsePosition);
    if (parsePosition.getIndex() != formattedValue.length()) {
        throw new RuntimeException("Invalid number " + formattedValue);
    }/*from w  w  w  . j  a va 2  s  .  c o m*/
    return o; //Double.parseDouble(formattedValue);
}