List of usage examples for java.text ParsePosition getIndex
public int getIndex()
From source file:routines.system.BigDataParserUtils.java
public synchronized static java.util.Date parseTo_Date(String s, String pattern) { if (isBlank(s)) { return null; }// w w w .ja v a 2 s . com String s2 = s.trim(); String pattern2 = pattern; if (isBlank(pattern2)) { pattern2 = Constant.dateDefaultPattern; } java.util.Date date = null; if (pattern2.equals("yyyy-MM-dd'T'HH:mm:ss'000Z'")) { if (!s2.endsWith("000Z")) { throw new RuntimeException("Unparseable date: \"" + s2 + "\""); //$NON-NLS-1$ //$NON-NLS-2$ } pattern2 = "yyyy-MM-dd'T'HH:mm:ss"; s2 = s.substring(0, s.lastIndexOf("000Z")); } DateFormat format = FastDateParser.getInstance(pattern2); ParsePosition pp = new ParsePosition(0); pp.setIndex(0); format.setTimeZone(TimeZone.getTimeZone("UTC")); date = format.parse(s2, pp); if (pp.getIndex() != s2.length() || date == null) { throw new RuntimeException("Unparseable date: \"" + s2 + "\""); //$NON-NLS-1$ //$NON-NLS-2$ } return date; }
From source file:routines.system.BigDataParserUtils.java
public synchronized static java.util.Date parseTo_Date(String s, String pattern, boolean lenient) { if (isBlank(s)) { return null; }/*from w w w. j a va2 s . c om*/ String s2 = s.trim(); String pattern2 = pattern; if (isBlank(pattern2)) { pattern2 = Constant.dateDefaultPattern; } java.util.Date date = null; if (pattern2.equals("yyyy-MM-dd'T'HH:mm:ss'000Z'")) { if (!s2.endsWith("000Z")) { throw new RuntimeException("Unparseable date: \"" + s2 + "\""); //$NON-NLS-1$ //$NON-NLS-2$ } pattern2 = "yyyy-MM-dd'T'HH:mm:ss"; s2 = s2.substring(0, s.lastIndexOf("000Z")); } DateFormat format = FastDateParser.getInstance(pattern2, lenient); ParsePosition pp = new ParsePosition(0); pp.setIndex(0); format.setTimeZone(TimeZone.getTimeZone("UTC")); date = format.parse(s2, pp); if (pp.getIndex() != s2.length() || date == null) { throw new RuntimeException("Unparseable date: \"" + s2 + "\""); //$NON-NLS-1$ //$NON-NLS-2$ } return date; }
From source file:org.kisoonlineapp.jsf.CalendarConverterInternal.java
@Override public Object getAsObject(FacesContext context, UIComponent component, String value) { final String trimmedValue = StringUtils.trimToNull(value); if (StringUtils.isBlank(trimmedValue)) { return null; }// w w w.j ava 2 s . com final SimpleDateFormat sdf = createSimpleDateFormat(context, pattern); sdf.setLenient(false); final ParsePosition pos = new ParsePosition(0); final Date date = sdf.parse(trimmedValue, pos); if (pos.getErrorIndex() >= 0 || pos.getIndex() != trimmedValue.length()) { throw new ConverterException("Cannot parse " + trimmedValue); } final Calendar cal = kisoOnlineApp.getNow(); cal.setTime(date); return cal; }
From source file:com.anrisoftware.globalpom.version.VersionFormat.java
/** * @see #parse(String, ParsePosition)/*from ww w . j a v a 2s . c o m*/ */ public Version parse(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Version result = parse(source, pos); if (pos.getIndex() == 0) { throw log.errorParse(source, pos); } return result; }
From source file:com.autonomy.aci.client.util.DateTimeUtils.java
/** * Parses a string representing a date, using the supplied pattern and locale. * <p>/*ww w . j a va 2 s . c o m*/ * A parse is only deemed successful if it parses the whole of the input string. If the parse pattern didn't match, a * ParseException is thrown. * @param string The date to parse, not null * @param format The date format pattern to use, see {@link java.text.SimpleDateFormat}, not null * @param locale The locale whose date format symbols should be used * @return The parsed date * @throws IllegalArgumentException If the date <tt>string</tt> or <tt>format</tt> are null * @throws ParseException If the date pattern was unsuitable */ public Date parseDate(final String string, final String format, final Locale locale) throws ParseException { LOGGER.trace("parseDate() called..."); Validate.notEmpty(string, "Date string must not be null"); Validate.notEmpty(format, "Date string format must not be null"); final SimpleDateFormat parser = new SimpleDateFormat(format, locale); final ParsePosition pos = new ParsePosition(0); final Date date = parser.parse(string, pos); if (date != null && pos.getIndex() == string.length()) { return date; } throw new ParseException("Unable to parse the date: " + string, -1); }
From source file:com.anrisoftware.globalpom.format.measurement.MeasureFormat.java
/** * @see #parse(String)//from www .j av a 2 s . co m * * @param pos * the index {@link ParsePosition} position from where to start * parsing. */ public Measure<?> parse(String source, ParsePosition pos) { source = source.substring(pos.getIndex()); try { Measure<?> address = parseValue(source, pos); pos.setErrorIndex(-1); pos.setIndex(source.length()); return address; } catch (ParseException e) { pos.setIndex(0); pos.setErrorIndex(0); return null; } }
From source file:org.osaf.cosmo.eim.schema.text.TriageStatusFormat.java
public Object parseObject(String source, ParsePosition pos) { if (pos.getIndex() > 0) return null; int index = 0; String[] chunks = source.split(" ", 3); if (chunks.length != 3) { parseException = new ParseException("Incorrect number of chunks: " + chunks.length, 0); pos.setErrorIndex(index);//from w w w .java 2s . c o m return null; } TriageStatus ts = entityFactory.createTriageStatus(); try { pos.setIndex(index); Integer code = new Integer(chunks[0]); // validate the code as being known TriageStatusUtil.label(code); ts.setCode(code); index += chunks[0].length() + 1; } catch (Exception e) { parseException = new ParseException(e.getMessage(), 0); pos.setErrorIndex(index); return null; } try { pos.setIndex(index); BigDecimal rank = new BigDecimal(chunks[1]); if (rank.scale() != 2) throw new NumberFormatException("Invalid rank value " + chunks[1]); ts.setRank(rank); index += chunks[1].length() + 1; } catch (NumberFormatException e) { parseException = new ParseException(e.getMessage(), 0); pos.setErrorIndex(index); return null; } if (chunks[2].equals(AUTOTRIAGE_ON)) ts.setAutoTriage(Boolean.TRUE); else if (chunks[2].equals(AUTOTRIAGE_OFF)) ts.setAutoTriage(Boolean.FALSE); else { parseException = new ParseException("Invalid autotriage value " + chunks[2], 0); pos.setErrorIndex(index); return null; } index += chunks[2].length(); pos.setIndex(index); return ts; }
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 { /*/*w w w.j a va 2 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:graph.module.DateParseModule.java
@SuppressWarnings("deprecation") private DAGNode parseDate(SimpleDateFormat sdf, String dateStr) { try {/*from www . j av a2s . c om*/ ParsePosition position = new ParsePosition(0); Date date = sdf.parse(dateStr, position); if (position.getIndex() != dateStr.length()) { // Throw an exception or whatever else you want to do return null; } String pattern = sdf.toPattern(); StringBuilder buffer = new StringBuilder(); boolean addFurther = false; int brackets = 0; if (addFurther || pattern.contains("d")) { addFurther = true; buffer.append("(" + CommonConcepts.DAYFN.getID() + " '" + date.getDate() + " "); brackets++; } if (addFurther || pattern.contains("M")) { addFurther = true; buffer.append("(" + CommonConcepts.MONTHFN.getID() + " " + MONTH_FORMATTER.format(date) + " "); brackets++; } if (pattern.contains("y")) { buffer.append("(" + CommonConcepts.YEARFN.getID() + " '" + (date.getYear() + 1900)); brackets++; } else if (addFurther) buffer.append(CommonConcepts.THE_YEAR.getID()); for (int i = 0; i < brackets; i++) buffer.append(")"); return (DAGNode) dag_.findOrCreateNode(buffer.toString(), null); } catch (Exception e) { } return null; }
From source file:com.anrisoftware.fractions.format.FractionFormat.java
/** * @see #parseObject(String)//from www . j a v a 2 s . c o m * * @param pos * the index {@link ParsePosition} position from where to start * parsing. * * @throws ParseException * if the string is not in the correct format. */ public ContinuedFraction parse(String source, ParsePosition pos) throws ParseException { try { source = source.substring(pos.getIndex()); ContinuedFraction f = decodeFraction(source, pos); pos.setErrorIndex(-1); pos.setIndex(pos.getIndex() + source.length()); return f; } catch (NumberFormatException e) { log.errorParseNumber(e, source); pos.setIndex(0); pos.setErrorIndex(0); return null; } }