List of usage examples for java.text ParsePosition setIndex
public void setIndex(int index)
From source file:Main.java
public static void main(String[] args) { String text = "ab01/01/1999cd12/31/2000ef"; String pattern = "MM/dd/yyyy"; SimpleDateFormat simpleFormatter = new SimpleDateFormat(pattern); // Set the start index at 2 ParsePosition startPos = new ParsePosition(2); // Parse the text to get the first date (January 1, 1999) Date firstDate = simpleFormatter.parse(text, startPos); System.out.println(firstDate); //Now, startPos has its index set after the last character of the first date parsed. int currentIndex = startPos.getIndex(); System.out.println(currentIndex); // To set its index to the next date increment its index by 2. int nextIndex = currentIndex + 2; startPos.setIndex(nextIndex); // Parse the text to get the second date (December 31, 2000) Date secondDate = simpleFormatter.parse(text, startPos); System.out.println(secondDate); }
From source file:Main.java
/** * <p>Parses a string representing a date by trying a variety of different parsers.</p> * // ww w. ja v a2s. com * <p>The parse will try each parse pattern in turn. * A parse is only deemed sucessful 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 * @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 */ public static Date parseDate(String str, String[] parsePatterns) throws ParseException { if (str == null || parsePatterns == null) { throw new IllegalArgumentException("Date and Patterns must not be null"); } SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0]); } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(str, pos); if (date != null && pos.getIndex() == str.length()) { return date; } } throw new ParseException("Unable to parse the date: " + str, -1); }
From source file:org.totschnig.myexpenses.Utils.java
/** * <a href="http://www.ibm.com/developerworks/java/library/j-numberformat/">http://www.ibm.com/developerworks/java/library/j-numberformat/</a> * @param strFloat parsed as float with the number format defined in the locale * @return the float retrieved from the string or null if parse did not succeed *//*from w ww . j a va 2 s .com*/ public static Float validateNumber(String strFloat) { ParsePosition pp; NumberFormat nfDLocal = NumberFormat.getNumberInstance(); nfDLocal.setGroupingUsed(false); pp = new ParsePosition(0); pp.setIndex(0); Number n = nfDLocal.parse(strFloat, pp); if (strFloat.length() != pp.getIndex() || n == null) { return null; } else { return n.floatValue(); } }
From source file:com.mgmtp.perfload.core.client.util.PlaceholderUtils.java
/** * Returns the next placeholder that can be parsed from the specified position in the specified * string.//from ww w . j a v a2s . c om * * @param input * the string to parse * @param pos * the position where parsing starts * @return the next placeholder available or null if none is found */ public static String parseNextPlaceholderName(final String input, final ParsePosition pos) { int index = pos.getIndex(); if (input.length() - index >= 3 && '$' == input.charAt(index) && '{' == input.charAt(index + 1)) { int start = index + 2; int end = input.indexOf('}', start); if (end < 0) { throw new IllegalStateException("Invalid placeholder: " + input.substring(index)); } pos.setIndex(end + 1); return start == end ? "" : input.substring(start, end); } return null; }
From source file:gov.nih.nci.cabig.caaers.utils.DateUtils.java
public static Date parseDate(String dateStr, String... parsePatterns) throws ParseException { if (dateStr == null || parsePatterns == null) { throw new IllegalArgumentException("Date and Patterns must not be null"); }//w w w . jav a 2 s . c o m String strDate = dateStr; //do year correction. (partial year >=50 will be 1999 and <50 will be 2000) String[] parts = StringUtils.split(dateStr, '/'); int len = parts.length; if (len != 3 || parts[0].length() > 2 || parts[1].length() > 2) throw new ParseException("Unable to parse the date " + strDate, -1); String yStr = parts[2]; if (!(yStr.length() == 4 || yStr.length() == 2 || yStr.length() == 10)) throw new ParseException("Unable to parse the date " + strDate, -1); if (yStr.length() == 2 && StringUtils.isNumeric(yStr)) { if (Integer.parseInt(yStr) < 50) yStr = "20" + yStr; else yStr = "19" + yStr; parts[2] = yStr; strDate = StringUtils.join(parts, '/'); } //BJ: date formats are not thread save, so we need to create one each time. SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0]); } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(strDate, pos); if (date != null && pos.getIndex() == strDate.length()) { return date; } } throw new ParseException("Unable to parse the date: " + strDate, -1); }
From source file:org.jumpmind.util.FormatUtils.java
public static Date parseDate(String str, String[] parsePatterns, TimeZone timeZone) { if (str == null || parsePatterns == null) { throw new IllegalArgumentException("Date and Patterns must not be null"); }//from ww w .java 2 s .co m SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0]); if (timeZone != null) { parser.setTimeZone(timeZone); } } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(str, pos); if (date != null && pos.getIndex() == str.length()) { return date; } } try { Date date = new Date(Long.parseLong(str)); return date; } catch (NumberFormatException e) { } throw new ParseException("Unable to parse the date: " + str); }
From source file:com.mgmtp.perfload.core.client.util.PlaceholderUtils.java
/** * Replaces all placeholders in the specified string using the specified map. If a placeholder * value cannot be found in the map, the placeholder is left as is. * // w w w .j a v a2 s. c o m * @param input * the string to parse * @param replacements * a map containing replacement values for placeholders * @return the string with all placeholders resolved */ public static String resolvePlaceholders(final String input, final Map<String, String> replacements) { if (isBlank(input)) { return input; } final int len = input.length(); ParsePosition pos = new ParsePosition(0); String result = resolveNextPlaceholder(input, pos, replacements); if (result != null && pos.getIndex() >= len) { // we are done if there was no next placeholder and the // parse position is already at the end of the string return result; } StringBuilder sb = new StringBuilder(len * 2); if (result == null) { // Add the character if no placeholder is at the current position // and increment the position sb.append(input.charAt(pos.getIndex())); pos.setIndex(pos.getIndex() + 1); } else { sb.append(result); } // loop as long as the parse position is less than the input string's length while (pos.getIndex() < len) { result = resolveNextPlaceholder(input, pos, replacements); if (result == null) { // Add the character if no placeholder is at the current position // and increment the position sb.append(input.charAt(pos.getIndex())); pos.setIndex(pos.getIndex() + 1); } else { sb.append(result); } } return sb.toString(); }
From source file:com.glaf.core.util.DateUtils.java
public static Date parseDate(String str, String[] parsePatterns) { if (str == null || parsePatterns == null) { throw new IllegalArgumentException("Date and Patterns must not be null"); }//w ww.j a va 2s . com SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { parser = new SimpleDateFormat(parsePatterns[0]); } else { parser.applyPattern(parsePatterns[i]); } pos.setIndex(0); Date date = parser.parse(str, pos); if (date != null && pos.getIndex() == str.length()) { return date; } } throw new RuntimeException("Unable to parse the date: " + str); }
From source file:org.opendatakit.briefcase.util.WebUtils.java
private static final Date parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz) { // borrowed from apache.commons.lang.DateUtils... Date d = null;//w ww. ja v a2s .c o m SimpleDateFormat parser = null; ParsePosition pos = new ParsePosition(0); for (int i = 0; i < parsePatterns.length; i++) { if (i == 0) { if (l == null) { parser = new SimpleDateFormat(parsePatterns[0]); } else { parser = new SimpleDateFormat(parsePatterns[0], l); } } else { parser.applyPattern(parsePatterns[i]); } parser.setTimeZone(tz); // enforce UTC for formats without timezones pos.setIndex(0); d = parser.parse(value, pos); if (d != null && pos.getIndex() == value.length()) { return d; } } return d; }
From source file:adalid.commons.util.TimeUtils.java
public static java.util.Date parse(String pdq) { if (StringUtils.isBlank(pdq)) { return null; }/*from ww w . j a v a2 s . c o m*/ String string = pdq.trim(); ParsePosition position = new ParsePosition(0); java.util.Date util = timestampFormatter().parse(string, position); int i = position.getIndex(); int l = string.length(); if (util != null && i == l) { return new Timestamp(util.getTime()); } position.setIndex(0); util = dateFormatter().parse(string, position); i = position.getIndex(); if (util != null) { if (i == l) { return new Date(util.getTime()); } java.util.Date time = timeFormatter().parse(string, position); i = position.getIndex(); if (time != null && i == l) { return merge(util, time); } } position.setIndex(0); util = timeFormatter().parse(string, position); i = position.getIndex(); if (util != null && i == l) { return new Time(util.getTime()); } return null; }