List of usage examples for java.text ParsePosition getIndex
public int getIndex()
From source file:MainClass.java
public static void main(String[] a) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String input[] = { "2013-10-01 Vancouver, B.C.", "1248-03-01 Ottawa, ON", "1323-06-06 Toronto, ON" }; for (int i = 0; i < input.length; i++) { ParsePosition pp = new ParsePosition(0); Date d = formatter.parse(input[i], pp); if (d == null) { System.err.println("Invalid date in " + input[i]); continue; }/* ww w.ja va 2s . c o m*/ String location = input[i].substring(pp.getIndex()); System.out.println(" on " + d + " in " + location); } }
From source file:DateParse2.java
public static void main(String[] args) { //+//from ww w. j a v a 2 s. c o m SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String input[] = { "BD: 1913-10-01 Vancouver, B.C.", "MD: 1948-03-01 Ottawa, ON", "DD: 1983-06-06 Toronto, ON" }; for (int i = 0; i < input.length; i++) { String aLine = input[i]; String action; switch (aLine.charAt(0)) { case 'B': action = "Born"; break; case 'M': action = "Married"; break; case 'D': action = "Died"; break; // others... default: System.err.println("Invalid code in " + aLine); continue; } int p = aLine.indexOf(' '); ParsePosition pp = new ParsePosition(p); Date d = formatter.parse(aLine, pp); if (d == null) { System.err.println("Invalid date in " + aLine); continue; } String location = aLine.substring(pp.getIndex()); System.out.println(action + " on " + d + " in " + location); } //- }
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);/*from w w w .ja v a 2 s.c o m*/ // 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
public static boolean isValidNumeric(String str) { NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos);//from ww w .j a v a 2s.c o m return str.length() == pos.getIndex(); }
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 w w w . ja v a2 s . com*/ * * @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:utils.StringManip.java
/** * Checks if a string is numeric//from w ww . j av a2 s . c o m * * @param str String to check if it is a number * @return True if the string is a number */ public static boolean isNumeric(String str) { NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos); boolean IsANumber = str.length() == pos.getIndex(); Pattern p = Pattern.compile("[0-9]"); Matcher m = p.matcher(str); boolean containsNumber = m.matches(); return IsANumber || containsNumber; }
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. * //from w ww . j a v a2s. 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.mgmtp.perfload.core.client.util.PlaceholderUtils.java
/** * Parses the next placeholder using {@link #parseNextPlaceholderName(String, ParsePosition)} * and resolves it from the specified map. If a placeholder value cannot be found in the map, * the placeholder is left as is.//from w ww.j av a2 s . c o m * * @param input * the string to parse * @param pos * the position where parsing starts * @param replacements * a map containing replacement values for placeholders * @return the replaced value */ public static String resolveNextPlaceholder(final String input, final ParsePosition pos, final Map<String, String> replacements) { if (isBlank(input)) { return input; } final int start = pos.getIndex(); if (start > input.length()) { return null; } String placeholderName = parseNextPlaceholderName(input, pos); if (placeholderName != null) { String result = replacements.get(placeholderName); if (result != null) { return result; } // Return the placeholder if no replacement was found return input.substring(start, pos.getIndex()); } return null; }
From source file:org.ossmeter.platform.communicationchannel.nntp.local.NntpUtil.java
public static Date parseDate(String dateString) { for (SimpleDateFormat sdf : sdfList) { ParsePosition ps = new ParsePosition(0); Date result = sdf.parse(dateString, ps); if (ps.getIndex() != 0) return result; }//from ww w. j av a 2 s . com System.err.println("\t\t" + dateString + " cannot be parsed!\n"); return null; }
From source file:org.sonar.api.utils.DateUtils.java
/** * @param s string in format {@link #DATE_FORMAT} * @throws SonarException when string cannot be parsed *//* ww w . j a va 2s . co m*/ public static Date parseDate(String s) { ParsePosition pos = new ParsePosition(0); Date result = THREAD_SAFE_DATE_FORMAT.parse(s, pos); if (pos.getIndex() != s.length()) { throw new SonarException("The date '" + s + "' does not respect format '" + DATE_FORMAT + "'"); } return result; }