Search in sources :

Example 11 with ParsePosition

use of java.text.ParsePosition in project jackson-databind by FasterXML.

the class ISO8601UtilsTest method testParseShortTime.

public void testParseShortTime() throws java.text.ParseException {
    Date d = ISO8601Utils.parse("2007-08-13T195123.789Z", new ParsePosition(0));
    assertEquals(date, d);
    d = ISO8601Utils.parse("2007-08-13T195123Z", new ParsePosition(0));
    assertEquals(dateZeroMillis, d);
    d = ISO8601Utils.parse("2007-08-13T215123.789+02:00", new ParsePosition(0));
    assertEquals(date, d);
}
Also used : Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 12 with ParsePosition

use of java.text.ParsePosition in project AntennaPod by AntennaPod.

the class DateUtils method parse.

public static Date parse(final String input) {
    if (input == null) {
        throw new IllegalArgumentException("Date must not be null");
    }
    String date = input.trim().replace('/', '-').replaceAll("( ){2,}+", " ");
    // if datetime is more precise than seconds, make sure the value is in ms
    if (date.contains(".")) {
        int start = date.indexOf('.');
        int current = start + 1;
        while (current < date.length() && Character.isDigit(date.charAt(current))) {
            current++;
        }
        // even more precise than microseconds: discard further decimal places
        if (current - start > 4) {
            if (current < date.length() - 1) {
                date = date.substring(0, start + 4) + date.substring(current);
            } else {
                date = date.substring(0, start + 4);
            }
        // less than 4 decimal places: pad to have a consistent format for the parser
        } else if (current - start < 4) {
            if (current < date.length() - 1) {
                date = date.substring(0, current) + StringUtils.repeat("0", 4 - (current - start)) + date.substring(current);
            } else {
                date = date.substring(0, current) + StringUtils.repeat("0", 4 - (current - start));
            }
        }
    }
    String[] patterns = { "dd MMM yy HH:mm:ss Z", "dd MMM yy HH:mm Z", "EEE, dd MMM yyyy HH:mm:ss Z", "EEE, dd MMM yyyy HH:mm:ss", "EEE, dd MMMM yyyy HH:mm:ss Z", "EEE, dd MMMM yyyy HH:mm:ss", "EEEE, dd MMM yyyy HH:mm:ss Z", "EEEE, dd MMM yy HH:mm:ss Z", "EEEE, dd MMM yyyy HH:mm:ss", "EEEE, dd MMM yy HH:mm:ss", "EEE MMM d HH:mm:ss yyyy", "EEE, dd MMM yyyy HH:mm Z", "EEE, dd MMM yyyy HH:mm", "EEE, dd MMMM yyyy HH:mm Z", "EEE, dd MMMM yyyy HH:mm", "EEEE, dd MMM yyyy HH:mm Z", "EEEE, dd MMM yy HH:mm Z", "EEEE, dd MMM yyyy HH:mm", "EEEE, dd MMM yy HH:mm", "EEE MMM d HH:mm yyyy", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS Z", "yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-ddZ", "yyyy-MM-dd" };
    SimpleDateFormat parser = new SimpleDateFormat("", Locale.US);
    parser.setLenient(false);
    parser.setTimeZone(defaultTimezone);
    ParsePosition pos = new ParsePosition(0);
    for (String pattern : patterns) {
        parser.applyPattern(pattern);
        pos.setIndex(0);
        try {
            Date result = parser.parse(date, pos);
            if (result != null && pos.getIndex() == date.length()) {
                return result;
            }
        } catch (Exception e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    }
    Log.d(TAG, "Could not parse date string \"" + input + "\" [" + date + "]");
    return null;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 13 with ParsePosition

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

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 = getAttribute(TAG_GPS_DATESTAMP);
    String time = getAttribute(TAG_GPS_TIMESTAMP);
    if (date == null || time == null || (!sNonZeroTimePattern.matcher(date).matches() && !sNonZeroTimePattern.matcher(time).matches())) {
        return -1;
    }
    String dateTimeString = date + ' ' + time;
    ParsePosition pos = new ParsePosition(0);
    try {
        Date datetime = sFormatter.parse(dateTimeString, pos);
        if (datetime == null)
            return -1;
        return datetime.getTime();
    } catch (IllegalArgumentException e) {
        return -1;
    }
}
Also used : Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 14 with ParsePosition

use of java.text.ParsePosition in project ORCID-Source by ORCID.

the class MigrateFundingAmountToANumericValue method getAmountAsBigDecimal.

private BigDecimal getAmountAsBigDecimal(String amount, String currencyCode, Locale locale) throws Exception {
    try {
        ParsePosition parsePosition = new ParsePosition(0);
        NumberFormat numberFormat = NumberFormat.getInstance(locale);
        Number number = null;
        if (!PojoUtil.isEmpty(currencyCode)) {
            Currency currency = Currency.getInstance(currencyCode);
            String currencySymbol = currency.getSymbol();
            number = numberFormat.parse(amount.replace(currencySymbol, StringUtils.EMPTY), parsePosition);
        } else {
            number = numberFormat.parse(amount, parsePosition);
        }
        if (parsePosition.getIndex() != amount.length())
            throw new Exception("Unable to parse amount into BigDecimal");
        return new BigDecimal(number.toString());
    } catch (Exception e) {
        throw e;
    }
}
Also used : Currency(java.util.Currency) BigDecimal(java.math.BigDecimal) ParsePosition(java.text.ParsePosition) NumberFormat(java.text.NumberFormat)

Example 15 with ParsePosition

use of java.text.ParsePosition in project jdk8u_jdk by JetBrains.

the class TestCharLiteralParser method test_parse_success.

@Test(dataProvider = "success")
public void test_parse_success(char c, boolean caseSensitive, String text, int pos, int expectedPos) {
    setCaseSensitive(caseSensitive);
    ParsePosition ppos = new ParsePosition(pos);
    TemporalAccessor parsed = getFormatter(c).parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getIndex(), expectedPos);
    } else {
        assertEquals(ppos.getIndex(), expectedPos);
        assertEquals(parsed.isSupported(YEAR), false);
        assertEquals(parsed.query(TemporalQueries.chronology()), null);
        assertEquals(parsed.query(TemporalQueries.zoneId()), null);
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

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