Search in sources :

Example 1 with ParsePosition

use of java.text.ParsePosition in project head by mifos.

the class LocalizationConverter method getDoubleValueForPercent.

private Double getDoubleValueForPercent(String doubleValueString) {
    if (decimalFormatForInterest == null) {
        loadDecimalFormats();
    }
    Double dNum = null;
    try {
        ParsePosition pp = new ParsePosition(0);
        Number num = decimalFormatForInterest.parse(doubleValueString, pp);
        if ((doubleValueString.length() != pp.getIndex()) || (num == null)) {
            throw new NumberFormatException("The format of the number is invalid. index " + pp.getIndex() + " locale " + locale.getCountry() + " " + locale.getLanguage());
        }
        dNum = num.doubleValue();
    } catch (Exception e) {
        throw new NumberFormatException(e.getMessage() + " .Number " + doubleValueString);
    }
    return dNum;
}
Also used : ParsePosition(java.text.ParsePosition)

Example 2 with ParsePosition

use of java.text.ParsePosition in project head by mifos.

the class LocalizationConverter method getDoubleValueForCurrentLocale.

public Double getDoubleValueForCurrentLocale(String doubleValueString) {
    Double dNum = null;
    try {
        ParsePosition pp = new ParsePosition(0);
        Number num = decimalFormatForMoney.parse(doubleValueString, pp);
        if ((doubleValueString.length() != pp.getIndex()) || (num == null)) {
            throw new NumberFormatException("The format of the number is invalid. index " + pp.getIndex() + " locale " + locale.getCountry() + " " + locale.getLanguage());
        }
        dNum = num.doubleValue();
    } catch (Exception e) {
        throw new NumberFormatException(e.getMessage() + " .Number " + doubleValueString);
    }
    return dNum;
}
Also used : ParsePosition(java.text.ParsePosition)

Example 3 with ParsePosition

use of java.text.ParsePosition in project android_frameworks_base by ParanoidAndroid.

the class ExifInterface method getDateTime.

/**
     * Returns number of milliseconds since Jan. 1, 1970, midnight.
     * Returns -1 if the date time information if not available.
     * @hide
     */
public long getDateTime() {
    String dateTimeString = mAttributes.get(TAG_DATETIME);
    if (dateTimeString == null)
        return -1;
    ParsePosition pos = new ParsePosition(0);
    try {
        Date datetime = sFormatter.parse(dateTimeString, pos);
        if (datetime == null)
            return -1;
        return datetime.getTime();
    } catch (IllegalArgumentException ex) {
        return -1;
    }
}
Also used : Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 4 with ParsePosition

use of java.text.ParsePosition in project android_frameworks_base by ParanoidAndroid.

the class ExifInterface method getGpsDateTime.

/**
     * Returns number of milliseconds since Jan. 1, 1970, midnight UTC.
     * Returns -1 if the date time information if not available.
     * @hide
     */
public long getGpsDateTime() {
    String date = mAttributes.get(TAG_GPS_DATESTAMP);
    String time = mAttributes.get(TAG_GPS_TIMESTAMP);
    if (date == null || time == null)
        return -1;
    String dateTimeString = date + ' ' + time;
    if (dateTimeString == null)
        return -1;
    ParsePosition pos = new ParsePosition(0);
    try {
        Date datetime = sFormatter.parse(dateTimeString, pos);
        if (datetime == null)
            return -1;
        return datetime.getTime();
    } catch (IllegalArgumentException ex) {
        return -1;
    }
}
Also used : Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 5 with ParsePosition

use of java.text.ParsePosition in project XobotOS by xamarin.

the class Timestamp method valueOf.

/**
     * Creates a {@code Timestamp} object with a time value equal to the time
     * specified by a supplied String holding the time in JDBC timestamp escape
     * format, which is {@code "yyyy-MM-dd HH:mm:ss.nnnnnnnnn}"
     *
     * @param s
     *            the {@code String} containing a time in JDBC timestamp escape
     *            format.
     * @return A {@code Timestamp} object with time value as defined by the
     *         supplied {@code String}.
     * @throws IllegalArgumentException
     *             if the provided string is {@code null}.
     */
public static Timestamp valueOf(String s) throws IllegalArgumentException {
    if (s == null) {
        throw new IllegalArgumentException("Argument cannot be null");
    }
    // omit trailing whitespace
    s = s.trim();
    if (!Pattern.matches(TIME_FORMAT_REGEX, s)) {
        throw badTimestampString(s);
    }
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    ParsePosition pp = new ParsePosition(0);
    /*
         * First parse out the yyyy-MM-dd HH:mm:ss component of the String into
         * a Date object using the SimpleDateFormat. This should stop after the
         * seconds value, according to the definition of SimpleDateFormat.parse,
         * with the ParsePosition indicating the index of the "." which should
         * precede the nanoseconds value
         */
    Date theDate;
    try {
        theDate = df.parse(s, pp);
    } catch (Exception e) {
        throw badTimestampString(s);
    }
    if (theDate == null) {
        throw badTimestampString(s);
    }
    /*
         * If we get here, the Date part of the string was OK - now for the
         * nanoseconds value. Strictly, this requires the remaining part of the
         * String to look like ".nnnnnnnnn". However, we accept anything with a
         * '.' followed by 1 to 9 digits - we also accept nothing (no fractions
         * of a second). Anything else is interpreted as incorrect format which
         * will generate an IllegalArgumentException
         */
    int position = pp.getIndex();
    int remaining = s.length() - position;
    int theNanos;
    if (remaining == 0) {
        // First, allow for the case where no fraction of a second is given:
        theNanos = 0;
    } else {
        /*
             * Case where fraction of a second is specified: Require 1 character
             * plus the "." in the remaining part of the string...
             */
        if ((s.length() - position) < ".n".length()) {
            throw badTimestampString(s);
        }
        /*
             * If we're strict, we should not allow any EXTRA characters after
             * the 9 digits
             */
        if ((s.length() - position) > ".nnnnnnnnn".length()) {
            throw badTimestampString(s);
        }
        // Require the next character to be a "."
        if (s.charAt(position) != '.') {
            throw new NumberFormatException("Bad input string format: expected '.' not '" + s.charAt(position) + "' in \"" + s + "\"");
        }
        // Get the length of the number string - need to account for the '.'
        int nanoLength = s.length() - position - 1;
        // Get the 9 characters following the "." as an integer
        String theNanoString = s.substring(position + 1, position + 1 + nanoLength);
        /*
             * We must adjust for the cases where the nanos String was not 9
             * characters long by padding out with zeros
             */
        theNanoString = theNanoString + "000000000";
        theNanoString = theNanoString.substring(0, 9);
        try {
            theNanos = Integer.parseInt(theNanoString);
        } catch (Exception e) {
            // If we get here, the string was not a number
            throw badTimestampString(s);
        }
    }
    if (theNanos < 0 || theNanos > 999999999) {
        throw badTimestampString(s);
    }
    Timestamp theTimestamp = new Timestamp(theDate.getTime());
    theTimestamp.setNanos(theNanos);
    return theTimestamp;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Aggregations

ParsePosition (java.text.ParsePosition)704 Test (org.junit.Test)253 Date (java.util.Date)221 TemporalAccessor (java.time.temporal.TemporalAccessor)163 SimpleDateFormat (java.text.SimpleDateFormat)125 DateTimeFormatter (java.time.format.DateTimeFormatter)94 Test (org.testng.annotations.Test)88 ParseException (java.text.ParseException)71 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)49 SimpleDateFormat (android.icu.text.SimpleDateFormat)39 FieldPosition (java.text.FieldPosition)32 Calendar (java.util.Calendar)26 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)25 Calendar (android.icu.util.Calendar)23 ULocale (android.icu.util.ULocale)23 NumberFormat (java.text.NumberFormat)23 Format (java.text.Format)21 DecimalFormat (java.text.DecimalFormat)19 GregorianCalendar (android.icu.util.GregorianCalendar)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)17