List of usage examples for java.text ParsePosition ParsePosition
public ParsePosition(int index)
From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java
/** * Checks if the value can safely be converted to an int primitive. * /*w w w . ja v a 2 s . c o m*/ * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if null) * @return the converted Integer value. */ public static Integer formatInt(String value, Locale locale) { Integer result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Integer.MIN_VALUE && num.doubleValue() <= Integer.MAX_VALUE) { result = Integer.valueOf(num.intValue()); } } } return result; }
From source file:com.ing.connector.interfaces.WExporter.java
public void setDownloadDate(String DownloadDate) { Date dt = null;/*from ww w. j a v a 2 s . com*/ ParsePosition pos = new ParsePosition(0); dt = formatter.parse(DownloadDate, pos); Calendar cal = Calendar.getInstance(); cal.setTime(dt); cal.set(Calendar.HOUR_OF_DAY, 3); //03:00 hours is what the old jdk set it to dt = cal.getTime(); m_downloadDate = new WDate(dt); }
From source file:org.apache.roller.weblogger.ui.rendering.pagers.AbstractWeblogEntriesPager.java
/** * Parse data as either 6-char or 8-char format. *//*from w w w .ja v a 2 s . c o m*/ protected Date parseDate(String dateString) { Date ret = null; SimpleDateFormat char8DateFormat = DateUtil.get8charDateFormat(); SimpleDateFormat char6DateFormat = DateUtil.get6charDateFormat(); Calendar cal = Calendar.getInstance(weblog.getTimeZoneInstance(), weblog.getLocaleInstance()); if (dateString != null && dateString.length() == 8 && StringUtils.isNumeric(dateString)) { char8DateFormat.setCalendar(cal); ParsePosition pos = new ParsePosition(0); ret = char8DateFormat.parse(dateString, pos); // make sure the requested date is not in the future Date today = getToday(); if (ret.after(today)) ret = today; } if (dateString != null && dateString.length() == 6 && StringUtils.isNumeric(dateString)) { char6DateFormat.setCalendar(cal); ParsePosition pos = new ParsePosition(0); ret = char6DateFormat.parse(dateString, pos); // make sure the requested date is not in the future Date today = getToday(); if (ret.after(today)) ret = today; } return ret; }
From source file:com.nextgis.mobile.forms.CameraFragment.java
private Date stringToDate(String aDate, String aFormat) { if (aDate == null) return null; ParsePosition pos = new ParsePosition(0); SimpleDateFormat simpledateformat = new SimpleDateFormat(aFormat); Date stringDate = simpledateformat.parse(aDate, pos); return stringDate; }
From source file:com.nridge.core.base.std.DatUtl.java
/** * Attempts to detect the date/time format of the value and create * a 'Date' object.// ww w .j a v a2s .c om * * @param aDateTimeValue String value. * @return Date object if the format is recognized or <i>null</i>. */ public static Date detectCreateDate(String aDateTimeValue) { Date createDate = null; if (StringUtils.isNotEmpty(aDateTimeValue)) { if (mDateFormatList == null) { mDateFormatList = new ArrayList<SimpleDateFormat>(); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_ISO8601DATETIME_DEFAULT)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_ISO8601DATETIME_MILLI2D)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_ISO8601DATETIME_MILLI3D)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_RFC1123_DATE_TIME)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_SQLORACLEDATE_DEFAULT)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_SQLISODATE_DEFAULT)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_SQLISOTIME_DEFAULT)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_SQLISODATETIME_DEFAULT)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_DATE_DEFAULT)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_TIME_AMPM)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_TIME_PLAIN)); mDateFormatList.add(new SimpleDateFormat(Field.FORMAT_TIMESTAMP_PACKED)); } ParsePosition parsePosition = new ParsePosition(0); for (SimpleDateFormat sdf : mDateFormatList) { sdf.setLenient(false); createDate = sdf.parse(aDateTimeValue, parsePosition); if (createDate != null) break; } } return createDate; }
From source file:com.anrisoftware.globalpom.format.degree.DegreeSexagesimalFormat.java
/** * @see #parse(String, ParsePosition)/* w ww .j ava2 s. c o m*/ */ public Amount<Angle> parse(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Amount<Angle> result = parse(source, pos); if (pos.getIndex() == 0) { throw log.errorParse(source, pos); } return result; }
From source file:org.easyrec.utils.MyUtils.java
/** * This function trie to parse a date given as string and returns a date if * successfull./*from ww w . ja v a 2s. c o m*/ */ public static Date dateFormatCheck(String dateString, SimpleDateFormat dateFormatter) { try { Date date = dateFormatter.parse(dateString, new ParsePosition(0)); Calendar c = Calendar.getInstance(); c.setTime(date); // mysql specific: cannot handle Dates with Years > 9999 if (c.get(Calendar.YEAR) < 10000) { return date; } else { return null; } } catch (Exception e) { logger.warn("An error occurred!", e); return null; } }
From source file:com.xylocore.copybook.runtime.converters.SimpleDateFormatDateConverter.java
@Override protected Object decodeExternalAlphanumeric(AlphanumericPICMarshaller aPICMarshaller, CopybookContext aContext, int aOffset, int aSize, int aFlags) { assert aPICMarshaller != null; assert aContext != null; String myString = aPICMarshaller.decodeAsString(aContext, aOffset, aSize, aFlags, null); Date myDate;/* w w w. j av a 2s. c o m*/ if (!aContext.isError()) { SimpleDateFormat myDateFormat = DateHelper.getThreadLocalSimpleDateFormat(pattern); ParsePosition myParsePosition = new ParsePosition(0); myDate = myDateFormat.parse(myString, myParsePosition); if (myDate == null) { aContext.setError(CopybookError.InvalidDateFormat, null); } } else { myDate = null; } return myDate; }
From source file:util.android.util.DateUtils.java
public static Date parseTime(String dateString) throws IllegalArgumentException { Date d = null;//from www .jav a2 s . c o m for (int n = 0; n < timeMasks.length; n++) { try { timeFormats[n].applyPattern(timeMasks[n]); timeFormats[n].setLenient(true); d = timeFormats[n].parse(dateString, new ParsePosition(0)); if (d != null) break; } catch (Exception ignored) { } } if (d == null) throw new IllegalArgumentException(); return d; }
From source file:com.scoreminion.GameAdapter.java
/** * Converts UTC date string to localized date string. * * @param utcDateStr the date string returned from the Scores API * @return the localized date string//from w w w . ja v a 2s .c om */ private String convertDateString(String utcDateStr) { Calendar rightNow = Calendar.getInstance(); TimeZone timeZone = rightNow.getTimeZone(); SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US); Date date = dateFormat.parse(utcDateStr, new ParsePosition(DATE_PARSE_POSITION_OFFSET)); Date adjustedDate = new Date(date.getTime() + timeZone.getOffset(rightNow.getTimeInMillis())); return dateFormat.format(adjustedDate); }