List of usage examples for java.text ParsePosition getIndex
public int getIndex()
From source file:org.sonar.api.utils.DateUtils.java
/** * @param s string in format {@link #DATETIME_FORMAT} * @throws SonarException when string cannot be parsed *///from w ww . ja v a 2 s . c o m public static Date parseDateTime(String s) { ParsePosition pos = new ParsePosition(0); Date result = THREAD_SAFE_DATETIME_FORMAT.parse(s, pos); if (pos.getIndex() != s.length()) { throw new SonarException("The date '" + s + "' does not respect format '" + DATETIME_FORMAT + "'"); } return result; }
From source file:de.odysseus.calyxo.base.util.ParseUtils.java
private static Object parse(Format format, String value) throws ParseException { ParsePosition pos = new ParsePosition(0); Object result = format.parseObject(value, pos); if (pos.getIndex() < value.length()) throw new ParseException("Cannot parse " + value + " (garbage suffix)!", pos.getIndex()); return result; }/*from w w w . j a v a 2 s . co m*/
From source file:jp.go.nict.langrid.management.web.utility.DateUtil.java
/** * //from ww w. java 2s.c o m * */ public static Date getDate(String text) throws ParseException { ParsePosition pp = new ParsePosition(0); if (text == null) { throw new ParseException("", pp.getIndex()); } int textLength = text.length(); for (SimpleDateFormat format : formats) { format.setLenient(false); pp = new ParsePosition(0); Date ret = format.parse(text, pp); if (textLength == pp.getIndex()) { return ret; } } throw new ParseException("", pp.getIndex()); }
From source file:jp.co.opentone.bsol.framework.core.util.DateUtil.java
/** * ???(?????????null)./*ww w . ja v a 2 s . c o m*/ * @param format ? * @param date * @return ?? */ public static Date convertStringToDate(String format, String date) { if (StringUtils.isEmpty(date)) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); ParsePosition pos = new ParsePosition(0); Date d = sdf.parse(date, pos); // ???????????? // ??????????? if (pos.getIndex() != date.length()) { return null; } return d; }
From source file:Main.java
/** * <p>Parses a string representing a date by trying a variety of different parsers.</p> * //from w w w . j a v a 2 s. 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.alfresco.util.CachingDateFormat.java
public static Pair<Date, Integer> lenientParse(String text, int minimumResolution) throws ParseException { DateTimeFormatter fmt = ISODateTimeFormat.dateTime(); try {/*from w ww . ja va2 s. c om*/ Date parsed = fmt.parseDateTime(text).toDate(); return new Pair<Date, Integer>(parsed, Calendar.MILLISECOND); } catch (IllegalArgumentException e) { } SimpleDateFormatAndResolution[] formatters = getLenientFormatters(); for (SimpleDateFormatAndResolution formatter : formatters) { if (formatter.resolution >= minimumResolution) { ParsePosition pp = new ParsePosition(0); Date parsed = formatter.simpleDateFormat.parse(text, pp); if ((pp.getIndex() < text.length()) || (parsed == null)) { continue; } return new Pair<Date, Integer>(parsed, formatter.resolution); } } throw new ParseException("Unknown date format", 0); }
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 w w. jav a2 s .co m*/ 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.zimbra.cs.util.SoapCLI.java
public static Date parseDatetime(String str) { for (String formatStr : DATETIME_FORMATS) { SimpleDateFormat fmt = new SimpleDateFormat(formatStr); fmt.setLenient(false);//from w w w.j av a 2 s . co m ParsePosition pp = new ParsePosition(0); Date d = fmt.parse(str, pp); if (d != null && pp.getIndex() == str.length()) return d; } return null; }
From source file:org.opendatakit.briefcase.util.WebUtils.java
/** * Parse a string into a datetime value. Tries the common Http formats, the * iso8601 format (used by Javarosa), the default formatting from * Date.toString(), and a time-only format. * /*w w w. j a v a 2 s . com*/ * @param value * @return */ public static final Date parseDate(String value) { if (value == null || value.length() == 0) return null; String[] iso8601Pattern = new String[] { PATTERN_ISO8601 }; String[] localizedParsePatterns = new String[] { // try the common HTTP date formats that have time zones PATTERN_RFC1123, PATTERN_RFC1036, PATTERN_DATE_TOSTRING }; String[] localizedNoTzParsePatterns = new String[] { // ones without timezones... (will assume UTC) PATTERN_ASCTIME }; String[] tzParsePatterns = new String[] { PATTERN_ISO8601, PATTERN_ISO8601_DATE, PATTERN_ISO8601_TIME }; String[] noTzParsePatterns = new String[] { // ones without timezones... (will assume UTC) PATTERN_ISO8601_WITHOUT_ZONE, PATTERN_NO_DATE_TIME_ONLY, PATTERN_YYYY_MM_DD_DATE_ONLY_NO_TIME_DASH }; Date d = null; // iso8601 parsing is sometimes off-by-one when JR does it... d = parseDateSubset(value, iso8601Pattern, null, TimeZone.getTimeZone("GMT")); if (d != null) return d; // try to parse with the JavaRosa parsers d = DateUtils.parseDateTime(value); if (d != null) return d; d = DateUtils.parseDate(value); if (d != null) return d; d = DateUtils.parseTime(value); if (d != null) return d; // try localized and english text parsers (for Web headers and interactive filter spec.) d = parseDateSubset(value, localizedParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT")); if (d != null) return d; d = parseDateSubset(value, localizedParsePatterns, null, TimeZone.getTimeZone("GMT")); if (d != null) return d; d = parseDateSubset(value, localizedNoTzParsePatterns, Locale.ENGLISH, TimeZone.getTimeZone("GMT")); if (d != null) return d; d = parseDateSubset(value, localizedNoTzParsePatterns, null, TimeZone.getTimeZone("GMT")); if (d != null) return d; // try other common patterns that might not quite match JavaRosa parsers d = parseDateSubset(value, tzParsePatterns, null, TimeZone.getTimeZone("GMT")); if (d != null) return d; d = parseDateSubset(value, noTzParsePatterns, null, TimeZone.getTimeZone("GMT")); if (d != null) return d; // try the locale- and timezone- specific parsers { DateFormat formatter = DateFormat.getDateTimeInstance(); ParsePosition pos = new ParsePosition(0); d = formatter.parse(value, pos); if (d != null && pos.getIndex() == value.length()) { return d; } } { DateFormat formatter = DateFormat.getDateInstance(); ParsePosition pos = new ParsePosition(0); d = formatter.parse(value, pos); if (d != null && pos.getIndex() == value.length()) { return d; } } { DateFormat formatter = DateFormat.getTimeInstance(); ParsePosition pos = new ParsePosition(0); d = formatter.parse(value, pos); if (d != null && pos.getIndex() == value.length()) { return d; } } throw new IllegalArgumentException("Unable to parse the date: " + value); }
From source file:org.apache.falcon.util.DateUtil.java
/** * Parses a datetime in ISO8601 format in the process timezone. * * @param s string with the datetime to parse. * @return the corresponding {@link java.util.Date} instance for the parsed date. * @throws java.text.ParseException thrown if the given string was * not an ISO8601 value for the process timezone. *///from ww w . j a v a 2 s. c om public static Date parseDateFalconTZ(String s) throws ParseException { s = s.trim(); ParsePosition pos = new ParsePosition(0); Date d = getISO8601DateFormat(activeTimeZone, activeTimeMask).parse(s, pos); if (d == null) { throw new ParseException("Could not parse [" + s + "] using [" + activeTimeMask + "] mask", pos.getErrorIndex()); } if (s.length() > pos.getIndex()) { throw new ParseException("Correct datetime string is followed by invalid characters: " + s, pos.getIndex()); } return d; }