List of usage examples for java.text ParsePosition ParsePosition
public ParsePosition(int index)
From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java
/** * Checks if the value can safely be converted to a byte primitive. * /*from ww w. j av a 2s. com*/ * @param value * The value validation is being performed on. * @param locale * The locale to use to parse the number (system default if null) * @return the converted Byte value. */ public static Byte formatByte(String value, Locale locale) { Byte result = null; if (value != null) { NumberFormat formatter = null; if (locale != null) { formatter = NumberFormat.getNumberInstance(locale); } else { formatter = NumberFormat.getNumberInstance(Locale.getDefault()); } formatter.setParseIntegerOnly(true); ParsePosition pos = new ParsePosition(0); Number num = formatter.parse(value, pos); // If there was no error and we used the whole string if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) { if (num.doubleValue() >= Byte.MIN_VALUE && num.doubleValue() <= Byte.MAX_VALUE) { result = Byte.valueOf(num.byteValue()); } } } return result; }
From source file:utils.StringManip.java
/** * Checks if a string is numeric/*from w ww. java2s . c om*/ * * @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:Main.java
/** * <p>Parses a string representing a date by trying a variety of different parsers.</p> * //from www . java 2 s . co m * <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:com.anrisoftware.globalpom.format.locale.LocaleFormat.java
/** * Parses the specified string to a locale. * /*w w w . ja v a2 s . c o m*/ * @return the parsed {@link Locale}. * * @throws ParseException * if the string cannot be parsed to a value. * * @see Locale#toString() */ public Locale parse(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Locale result = parse(source, pos); if (pos.getIndex() == 0) { throw log.errorParse(source, pos); } return result; }
From source file:org.createnet.raptor.models.data.types.NumberRecord.java
@Override public Number parseValue(Object value) { try {//from ww w . j a v a2s .c o m if (value instanceof Number) { return (Number) value; } if (value instanceof JsonNode) { JsonNode node = (JsonNode) value; if (node.isNumber()) { if (node.isInt() || node.isShort()) { return (Number) node.asInt(); } if (node.isDouble() || node.isFloat()) { return (Number) node.asDouble(); } if (node.isLong()) { return (Number) node.asLong(); } } if (node.isTextual()) { value = node.asText(); } } NumberFormat formatter = NumberFormat.getInstance(); ParsePosition pos = new ParsePosition(0); Number numVal = formatter.parse((String) value, pos); return numVal; } catch (Exception e) { throw new RaptorComponent.ParserException(e); } }
From source file:org.jboss.dashboard.ui.resources.ResourceName.java
public static ResourceName getInstance(String resName) { ResourceName resource = new ResourceName(); try {//w ww . j a va 2 s.c o m //Workspace ParsePosition pPos = new ParsePosition(0); String resourceName = useBase64Names ? new String(Base64.decode(resName)) : resName; Object[] o = msgf.parse(resourceName, pPos); if (o == null) throw new ParseException( "Cannot parse " + resourceName + ". Error at position " + pPos.getErrorIndex(), pPos.getErrorIndex()); //log.debug("Parsing result: " + Arrays.asList(o)); //log.debug("Workspace name determined from resource name is " + o[0]); resource.workspaceId = (String) o[0]; resource.workspaceId = "".equals(resource.workspaceId) ? null : resource.workspaceId; //Section //log.debug("Section determined from resource name is " + o[1]); String sectionId = (String) o[1]; if (!"".equals(sectionId)) resource.sectionId = new Long(sectionId); //Panel //log.debug("Panel determined from resource name is " + o[2]); String panelId = (String) o[2]; if (!"".equals(panelId)) resource.panelId = new Long(panelId); //Category //log.debug("Category determined from resource name is " + o[3]); resource.category = (String) o[3]; if ("".equals(resource.category)) throw new ParseException("Cannot find non-empty category name", 3); //CategoryId //log.debug("CategoryId determined from resource name is " + o[4]); resource.categoryId = (String) o[4]; resource.categoryId = "".equals(resource.categoryId) ? null : resource.categoryId; //ResourceId //log.debug("Resource determined from resource name is " + o[5]); resource.resourceId = (String) o[5]; if ("".equals(resource.resourceId)) resource.resourceId = null; StringBuffer sb = new StringBuffer(); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.workspaceId)); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.sectionId)); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.panelId)); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.category)); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.categoryId)); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.resourceId)); resource.resName = useBase64Names ? Base64.encode(sb.toString().getBytes()) : sb.toString(); /*if (resource.categoryId != null) { sb=new StringBuffer(); sb.append(SEPARATOR); sb.append(SEPARATOR); sb.append(SEPARATOR); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.category)); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.categoryId)); sb.append(SEPARATOR); sb.append(RenderUtils.noNull(resource.resourceId)); resource.portableResourceName = useBase64Names ? Base64.encode(sb.toString().getBytes()) : sb.toString(); } else{ resource.portableResourceName = resource.resName; log.debug("Resource "+resource.resName+" is not portable."); } */ resource.portableResourceName = resource.resName; resource.resourceClass = Class.forName("org.jboss.dashboard.ui.resources." + resource.category.substring(0, 1).toUpperCase() + resource.category.substring(1)); } catch (Exception e) { log.debug("Error processing resource name. ", e); resource = null; } return resource; }
From source file:org.catechis.Transformer.java
/** *Takes a string with the date format we have been using since the beginning, *and returns a long representing the instant. *//*from w w w. j a v a 2s .c o m*/ public static String getLongDateAsString(String str_date) { SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); Date in_date = null; ParsePosition pp = new ParsePosition(0); Date date = sdf.parse(str_date, pp); String result = Long.toString(date.getTime()); return result; }
From source file:be.wegenenverkeer.common.resteasy.json.Iso8601AndOthersLocalDateTimeFormat.java
/** * Parse string to date./* ww w. ja v a 2s . c om*/ * * @param str string to parse * @return date */ public LocalDateTime parse(String str) { LocalDateTime date = null; if (StringUtils.isNotBlank(str)) { // try full ISO 8601 format first try { Date timestamp = ISO8601Utils.parse(str, new ParsePosition(0)); Instant instant = Instant.ofEpochMilli(timestamp.getTime()); return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); } catch (IllegalArgumentException | ParseException ex) { // ignore, try next format date = null; // dummy } // then try ISO 8601 format without timezone try { return LocalDateTime.from(iso8601NozoneFormat.parse(str)); } catch (IllegalArgumentException | DateTimeParseException ex) { // ignore, try next format date = null; // dummy } // then try a list of formats for (DateTimeFormatter formatter : FORMATS) { try { TemporalAccessor ta = formatter.parse(str); try { return LocalDateTime.from(ta); } catch (DateTimeException dte) { return LocalDate.from(ta).atStartOfDay(); } } catch (IllegalArgumentException | DateTimeParseException e) { // ignore, try next format date = null; // dummy } } throw new IllegalArgumentException("Could not parse date " + str + " using ISO 8601 or any of the formats " + Arrays.asList(FORMATS) + "."); } return date; // empty string }
From source file:com.haulmont.chile.core.datatypes.impl.NumberDatatype.java
protected Number parse(String value, NumberFormat format) throws ParseException { ParsePosition pos = new ParsePosition(0); Number res = format.parse(value.trim(), pos); if (pos.getIndex() != value.length()) { throw new ParseException(String.format("Unparseable number: \"%s\"", value), pos.getErrorIndex()); }/*www . j a va 2 s . c om*/ return res; }
From source file:org.netxilia.api.impl.value.NumberParser.java
@Override public IGenericValue parse(String text) { String input = preprocess(text); ParsePosition pp = new ParsePosition(0); for (NumberFormat format : this.formats) { pp.setIndex(0);//from ww w.j a v a2s.c o m Number number = format.parse(input, pp); if (number != null && input.length() == pp.getIndex()) { return new NumberValue(number.doubleValue()); } } return null; }