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);
}
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;
}
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;
}
}
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;
}
}
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);
}
}
Aggregations