List of usage examples for java.text ParsePosition setIndex
public void setIndex(int index)
From source file:ISO8601DateFormat.java
/** * @see java.text.DateFormat#parse(String, ParsePosition) *//*from w ww.j ava 2s. c o m*/ public Date parse(String text, ParsePosition pos) { int i = pos.getIndex(); try { int year = Integer.valueOf(text.substring(i, i + 4)).intValue(); i += 4; if (text.charAt(i) != '-') { throw new NumberFormatException(); } i++; int month = Integer.valueOf(text.substring(i, i + 2)).intValue() - 1; i += 2; if (text.charAt(i) != '-') { throw new NumberFormatException(); } i++; int day = Integer.valueOf(text.substring(i, i + 2)).intValue(); i += 2; calendar.set(year, month, day); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); // no parts of a second i = parseTZ(i, text); } catch (NumberFormatException ex) { pos.setErrorIndex(i); return null; } catch (IndexOutOfBoundsException ex) { pos.setErrorIndex(i); return null; } finally { pos.setIndex(i); } return calendar.getTime(); }
From source file:Unsigned.java
/** * Parse a binary number, skipping leading whitespace. Does not throw an * exception; if no object can be parsed, index is unchanged! * /* ww w .ja v a 2s . c o m*/ * @param source * the string to parse * @param status * the string index to start at * @return The binary number as a Long object. * * @since 1.0 */ public Object parseObject(String source, ParsePosition status) { int start = status.getIndex(); boolean success = false; boolean skipWhitespace = true; StringBuffer buffer = new StringBuffer(); StringCharacterIterator iter = new StringCharacterIterator(source, start); for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) { if (skipWhitespace && Character.isWhitespace(c)) { // skip whitespace continue; } skipWhitespace = false; if ((c == '1') || (c == '0')) { success = true; buffer.append(c); } else { break; } } if (!success) { return (null); } // convert binary to long if (buffer.length() > 64) { // larger than a long, error return (null); } long result = 0; buffer.reverse(); int length = buffer.length(); for (int i = 0; i < length; i++) { result += (buffer.charAt(i) == '1') ? 1 << i : 0; } status.setIndex(iter.getIndex()); return (new Long(result)); }
From source file:DateFormatUtils.java
/** * <p>Parsing is not supported.</p> * /*from w w w . ja va 2s. co m*/ * @param source the string to parse * @param pos the parsing position * @return <code>null</code> as not supported */ public Object parseObject(String source, ParsePosition pos) { pos.setIndex(0); pos.setErrorIndex(0); return null; }
From source file:ISO8601DateTimeFormat.java
/** * @see DateFormat#parse(String, ParsePosition) *//*ww w. j ava 2 s. co m*/ public Date parse(String text, ParsePosition pos) { int i = pos.getIndex(); try { int year = Integer.valueOf(text.substring(i, i + 4)).intValue(); i += 4; if (text.charAt(i) != '-') { throw new NumberFormatException(); } i++; int month = Integer.valueOf(text.substring(i, i + 2)).intValue() - 1; i += 2; if (text.charAt(i) != '-') { throw new NumberFormatException(); } i++; int day = Integer.valueOf(text.substring(i, i + 2)).intValue(); i += 2; if (text.charAt(i) != 'T') { throw new NumberFormatException(); } i++; int hour = Integer.valueOf(text.substring(i, i + 2)).intValue(); i += 2; if (text.charAt(i) != ':') { throw new NumberFormatException(); } i++; int mins = Integer.valueOf(text.substring(i, i + 2)).intValue(); i += 2; int secs = 0; if (i < text.length() && text.charAt(i) == ':') { // handle seconds flexible i++; secs = Integer.valueOf(text.substring(i, i + 2)).intValue(); i += 2; } calendar.set(year, month, day, hour, mins, secs); calendar.set(Calendar.MILLISECOND, 0); // no parts of a second i = parseTZ(i, text); } catch (NumberFormatException ex) { pos.setErrorIndex(i); return null; } catch (IndexOutOfBoundsException ex) { pos.setErrorIndex(i); return null; } finally { pos.setIndex(i); } return calendar.getTime(); }
From source file:HexFormat.java
/** * Parse a hexadecimal number, skipping leading whitespace. Does not throw * an exception; if no object can be parsed, index is unchanged! Hexadecimal * numbers may be indicated with a leading character designation of '0x'. * /*from w ww .j a v a 2s.c om*/ * @param source * the string to parse * @param status * the string index to start at * @return The hexadecimal number as a Long object. * * @since 1.0 */ public Object parseObject(String source, ParsePosition status) { int start = status.getIndex(); boolean success = false; StringBuffer buffer = new StringBuffer(); char c, c2; long result; StringCharacterIterator iter = new StringCharacterIterator(source, start); for (c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) { if (Character.isWhitespace(c)) { // skip whitespace continue; } break; } if (c == CharacterIterator.DONE) { return (null); } if (c == '0') { c2 = iter.next(); if (c2 == CharacterIterator.DONE) { return (null); } if (c2 == 'x') { // has a leading '0x' designation, so skip over it } else { // replace the two characters iter.previous(); iter.previous(); } } else { // skip back one character iter.previous(); } // gather valid hex digits for (c = iter.next(); c != CharacterIterator.DONE; c = iter.next()) { if (hexDigits.indexOf(c) != -1) { success = true; buffer.append(c); } else { break; } } if (!success) { // no valid hex digits return (null); } // convert hex to long if (buffer.length() > 16) { // larger than a long, error // with a buffer full of nibbles, the maximum nibbles in a // 64 bit number is 16 nibbles return (null); } // parse number try { result = Long.parseLong(buffer.toString(), 16); } catch (NumberFormatException e) { // unable to parse number return (null); } status.setIndex(iter.getIndex()); return (new Long(result)); }
From source file:com.clark.func.Functions.java
/** * <p>//w w w . j a v a 2 s.com * Parses a string representing a date by trying a variety of different * parsers. * </p> * * <p> * The parse will try each parse pattern in turn. A parse is only deemed * successful if it parses the whole of the input string. If no parse * patterns match, a ParseException is thrown. * </p> * * @param str * the date to parse, not null * @param parsePatterns * the date format patterns to use, see SimpleDateFormat, not * null * @param lenient * Specify whether or not date/time parsing is to be lenient. * @return the parsed date * @throws IllegalArgumentException * if the date string or pattern array is null * @throws ParseException * if none of the date patterns were suitable * @see java.util.Calender#isLenient() */ private static Date parseDateWithLeniency(String str, String[] parsePatterns, boolean lenient) throws ParseException { if (str == null || parsePatterns == null) { throw new IllegalArgumentException("Date and Patterns must not be null"); } SimpleDateFormat parser = new SimpleDateFormat(); parser.setLenient(lenient); ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { String pattern = parsePatterns[i]; // LANG-530 - need to make sure 'ZZ' output doesn't get passed to // SimpleDateFormat if (parsePatterns[i].endsWith("ZZ")) { pattern = pattern.substring(0, pattern.length() - 1); } parser.applyPattern(pattern); pos.setIndex(0); String str2 = str; // LANG-530 - need to make sure 'ZZ' output doesn't hit // SimpleDateFormat as it will ParseException if (parsePatterns[i].endsWith("ZZ")) { str2 = str.replaceAll("([-+][0-9][0-9]):([0-9][0-9])$", "$1$2"); } Date date = parser.parse(str2, pos); if (date != null && pos.getIndex() == str2.length()) { return date; } } throw new ParseException("Unable to parse the date: " + str, -1); }