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.orcid.frontend.web.controllers.FundingsController.java

/**
 * Transforms a string into a BigDecimal
 * /*from w w  w  . j  a  v a  2  s . c om*/
 * @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:org.catechis.Stats.java

/** This method just gets the difference between the two dates
* in milliseconds./*from  w  w w  . ja  va  2s.  co m*/
*<p>If we re-use the date format and parse position we get a null pointer exception.
*/
private long getDateDelta(String later_date, String early_date) {
    //log.add("--- later "+later_date+" early "+early_date);
    long delta = 0;
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    ParsePosition pp = new ParsePosition(0);
    Date early = sdf.parse(early_date, pp);
    long early_milliseconds = early.getTime();
    SimpleDateFormat lsdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    ParsePosition lpp = new ParsePosition(0);
    Date later = lsdf.parse(later_date, lpp);
    long later_milliseconds = later.getTime();
    delta = (later_milliseconds - early_milliseconds);
    //log.add(later_milliseconds+" - "+early_milliseconds+" = "+delta);
    return delta;
}

From source file:org.power.commons.lang.util.time.DateUtils.java

/**
 * fotmat, Stringutil.Date/*from   w  w w  .  ja  v  a  2 s  .  c  om*/
 *
 * @param strDate
 * @param format
 * @return
 * @author kanghongwei
 */
public static Date strToDateWithFormat(String strDate, Format format) {
    SimpleDateFormat localFormat = (SimpleDateFormat) format;
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = localFormat.parse(strDate, pos);
    return strtodate;
}

From source file:org.op4j.functions.FnCalendar.java

protected static Calendar fromInts(final Integer year, final Integer month, final Integer day,
        final Integer hour, final Integer minute, final Integer second, final Integer milli) throws Exception {

    /*/*from w  w  w  . ja  v a  2s.c  om*/
     * None of the Integers can be null 
     */
    Validate.notNull(year);
    Validate.notNull(month);
    Validate.notNull(day);
    Validate.notNull(hour);
    Validate.notNull(minute);
    Validate.notNull(second);
    Validate.notNull(milli);

    Integer safeYear = year;
    String yearAsString = year.toString();
    if ((safeYear.intValue() >= 0) && (yearAsString.length() <= 2)) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yy");
        final Calendar calendar = Calendar.getInstance();

        //It uses ParsePosition to make sure the whole 
        //string has been converted into a number
        ParsePosition pp = new ParsePosition(0);
        Date date = sdf.parse(yearAsString, pp);
        if (pp.getIndex() != yearAsString.length()) {
            throw new ParseException("The whole input String does not represent a valid Date", pp.getIndex());
        }
        calendar.setTime(date);
        safeYear = Integer.valueOf(calendar.get(Calendar.YEAR));
    }

    final Calendar result = Calendar.getInstance();
    result.set(Calendar.YEAR, safeYear.intValue());
    result.set(Calendar.MONTH, month.intValue() - 1);
    result.set(Calendar.DAY_OF_MONTH, day.intValue());
    result.set(Calendar.HOUR_OF_DAY, hour.intValue());
    result.set(Calendar.MINUTE, minute.intValue());
    result.set(Calendar.SECOND, second.intValue());
    result.set(Calendar.MILLISECOND, milli.intValue());

    return result;
}

From source file:org.op4j.functions.FnDate.java

protected static Date fromInts(final Integer year, final Integer month, final Integer day, final Integer hour,
        final Integer minute, final Integer second, final Integer milli) throws Exception {

    /*//from  w w  w. ja v  a2 s .co m
     * None of the Integers can be null 
     */
    Validate.notNull(year);
    Validate.notNull(month);
    Validate.notNull(day);
    Validate.notNull(hour);
    Validate.notNull(minute);
    Validate.notNull(second);
    Validate.notNull(milli);

    Integer safeYear = year;
    String yearAsString = year.toString();
    if ((safeYear.intValue() >= 0) && (yearAsString.length() <= 2)) {
        final SimpleDateFormat sdf = new SimpleDateFormat("yy");
        final Calendar calendar = Calendar.getInstance();

        //It uses ParsePosition to make sure the whole 
        //string has been converted into a number
        ParsePosition pp = new ParsePosition(0);
        Date date = sdf.parse(yearAsString, pp);
        if (pp.getIndex() != yearAsString.length()) {
            throw new ParseException("The whole input String does not represent a valid Date", pp.getIndex());
        }
        calendar.setTime(date);
        safeYear = Integer.valueOf(calendar.get(Calendar.YEAR));
    }

    final Calendar result = Calendar.getInstance();
    result.set(Calendar.YEAR, safeYear.intValue());
    result.set(Calendar.MONTH, month.intValue() - 1);
    result.set(Calendar.DAY_OF_MONTH, day.intValue());
    result.set(Calendar.HOUR_OF_DAY, hour.intValue());
    result.set(Calendar.MINUTE, minute.intValue());
    result.set(Calendar.SECOND, second.intValue());
    result.set(Calendar.MILLISECOND, milli.intValue());

    return result.getTime();
}

From source file:com.novartis.opensource.yada.adaptor.JDBCAdaptor.java

/**
* Sets a {@code ?t} parameter value mapped to the correct {@link java.sql.Types#TIMESTAMP} JDBC setter.
* @param pstmt the statement in which to set the parameter values
* @param index the current parameter//  www.j av a  2  s. c o m
* @param type the data type of the parameter (retained here for logging)
* @param val the value to set
* @throws SQLException when a parameter cannot be set, for instance if the data type is wrong or unsupported
* @since 5.1.0
*/
protected void setTimestampParameter(PreparedStatement pstmt, int index, char type, String val)
        throws SQLException {
    if (EMPTY.equals(val) || val == null) {
        pstmt.setNull(index, java.sql.Types.TIMESTAMP);
    } else {
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_TIMESTAMP_FMT);
        ParsePosition pp = new ParsePosition(0);
        Date dateVal = sdf.parse(val, pp);
        if (dateVal != null) {
            long t = dateVal.getTime();
            java.sql.Timestamp sqlDateVal = new java.sql.Timestamp(t);
            pstmt.setTimestamp(index, sqlDateVal);
        }
    }
}

From source file:com.amaze.filemanager.utils.files.FileUtils.java

/**
 * We're parsing a line returned from a stdout of shell.
 * @param line must be the line returned from a 'ls' command
 *///  ww w.  ja  va2 s .  com
public static HybridFileParcelable parseName(String line) {
    boolean linked = false;
    StringBuilder name = new StringBuilder();
    StringBuilder link = new StringBuilder();
    String size = "-1";
    String date = "";
    String[] array = line.split(" ");
    if (array.length < 6)
        return null;
    for (String anArray : array) {
        if (anArray.contains("->") && array[0].startsWith("l")) {
            linked = true;
        }
    }
    int p = getColonPosition(array);
    if (p != -1) {
        date = array[p - 1] + " | " + array[p];
        size = array[p - 2];
    }
    if (!linked) {
        for (int i = p + 1; i < array.length; i++) {
            name.append(" ").append(array[i]);
        }
        name = new StringBuilder(name.toString().trim());
    } else {
        int q = getLinkPosition(array);
        for (int i = p + 1; i < q; i++) {
            name.append(" ").append(array[i]);
        }
        name = new StringBuilder(name.toString().trim());
        for (int i = q + 1; i < array.length; i++) {
            link.append(" ").append(array[i]);
        }
    }
    long Size = (size == null || size.trim().length() == 0) ? -1 : Long.parseLong(size);
    if (date.trim().length() > 0) {
        ParsePosition pos = new ParsePosition(0);
        SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd | HH:mm");
        Date stringDate = simpledateformat.parse(date, pos);
        HybridFileParcelable baseFile = new HybridFileParcelable(name.toString(), array[0],
                stringDate.getTime(), Size, true);
        baseFile.setLink(link.toString());
        return baseFile;
    } else {
        HybridFileParcelable baseFile = new HybridFileParcelable(name.toString(), array[0],
                new File("/").lastModified(), Size, true);
        baseFile.setLink(link.toString());
        return baseFile;
    }
}

From source file:com.novartis.opensource.yada.adaptor.JDBCAdaptor.java

/**
* Sets a {@code ?d} parameter value mapped to the correct {@link java.sql.Types#DATE} JDBC setter.
* @param pstmt the statement in which to set the parameter values
* @param index the current parameter//from  w w  w .jav a2  s.com
* @param type the data type of the parameter (retained here for logging)
* @param val the value to set
* @throws SQLException when a parameter cannot be set, for instance if the data type is wrong or unsupported
* @since 5.1.0
*/
protected void setDateParameter(PreparedStatement pstmt, int index, char type, String val) throws SQLException {
    if (EMPTY.equals(val) || val == null) {
        pstmt.setNull(index, java.sql.Types.DATE);
    } else {
        SimpleDateFormat sdf = new SimpleDateFormat(STANDARD_DATE_FMT);
        ParsePosition pp = new ParsePosition(0);
        Date dateVal = sdf.parse(val, pp);
        if (dateVal != null) {
            long t = dateVal.getTime();
            java.sql.Date sqlDateVal = new java.sql.Date(t);
            pstmt.setDate(index, sqlDateVal);
        }
    }
}

From source file:com.amaze.carbonfilemanager.utils.files.Futils.java

/**
 * We're parsing a line returned from a stdout of shell.
 * @param line must be the line returned from a 'ls' command
 * @return//  www.ja va 2s  .c o  m
 */
public static BaseFile parseName(String line) {
    boolean linked = false;
    String name = "", link = "", size = "-1", date = "";
    String[] array = line.split(" ");
    if (array.length < 6)
        return null;
    for (int i = 0; i < array.length; i++) {
        if (array[i].contains("->") && array[0].startsWith("l")) {
            linked = true;
        }
    }
    int p = getColonPosition(array);
    if (p != -1) {
        date = array[p - 1] + " | " + array[p];
        size = array[p - 2];
    }
    if (!linked) {
        for (int i = p + 1; i < array.length; i++) {
            name = name + " " + array[i];
        }
        name = name.trim();
    } else {
        int q = getLinkPosition(array);
        for (int i = p + 1; i < q; i++) {
            name = name + " " + array[i];
        }
        name = name.trim();
        for (int i = q + 1; i < array.length; i++) {
            link = link + " " + array[i];
        }
    }
    long Size = (size == null || size.trim().length() == 0) ? -1 : Long.parseLong(size);
    if (date.trim().length() > 0) {
        ParsePosition pos = new ParsePosition(0);
        SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd | HH:mm");
        Date stringDate = simpledateformat.parse(date, pos);
        BaseFile baseFile = new BaseFile(name, array[0], stringDate.getTime(), Size, true);
        baseFile.setLink(link);
        return baseFile;
    } else {
        BaseFile baseFile = new BaseFile(name, array[0], new File("/").lastModified(), Size, true);
        baseFile.setLink(link);
        return baseFile;
    }
}

From source file:org.catechis.Stats.java

/**We convert the string with DateFormat.parse and get the
* calendar object and use the after method to determine which
* is the most recent date, and either return the new date, or
* the previously set date.  We store both the string date and
* the number of milliseconds since January 1, 1970, 00:00:00 GMT
* used in the comparison./*from  ww  w  .  jav  a 2s .  c  o  m*/
*<p>Each date was created like this:
* Date date = new Date();
* String str_date = date.toString();
*<p>Using date = df.parse(str_date) w/o the ParsePosition object needs
* to catch a java.text.ParseException.
*<p> SAMPLE DATE : Mon Aug 15 08:09:00 PST 2005
*/
private void compareDates(String str_date) {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    Date date = null;
    ParsePosition pp = new ParsePosition(0);
    date = sdf.parse(str_date, pp);
    long this_time = date.getTime();
    if (this_time > last_time) {
        last_time = this_time;
        last_date = str_date;
    } else {
        if (last_date == null) {
            last_time = this_time;
            last_date = str_date;
        } else {
            // last_time should already be set
        }
    }
}