List of usage examples for java.text ParseException ParseException
public ParseException(String s, int errorOffset)
From source file:org.tinymediamanager.core.tvshow.entities.TvShowEpisode.java
/** * convenient method to set the first aired date (parsed from string). * /*from w w w .ja v a 2s . c om*/ * @param aired * the new first aired * @throws ParseException * if string cannot be parsed! */ public void setFirstAired(String aired) throws ParseException { Pattern date = Pattern.compile("([0-9]{2})[_\\.-]([0-9]{2})[_\\.-]([0-9]{4})"); Matcher m = date.matcher(aired); if (m.find()) { this.firstAired = new SimpleDateFormat("dd-MM-yyyy") .parse(m.group(1) + "-" + m.group(2) + "-" + m.group(3)); } else { date = Pattern.compile("([0-9]{4})[_\\.-]([0-9]{2})[_\\.-]([0-9]{2})"); m = date.matcher(aired); if (m.find()) { this.firstAired = new SimpleDateFormat("yyyy-MM-dd") .parse(m.group(1) + "-" + m.group(2) + "-" + m.group(3)); } else { throw new ParseException("could not parse date from: " + aired, 0); } } }
From source file:org.wso2.carbon.siddhi.tryit.ui.SiddhiTryItClient.java
/** * Create time stamp for the given date and time * * @param dateTime date and time to begin the process *///from w w w .j a v a2 s . co m private long createTimeStamp(String dateTime) throws ParseException { Date date; try { date = dateFormatter.parse(dateTime); } catch (ParseException e) { errMsg = "Error occurred while parsing date " + e.getMessage(); log.error(errMsg, e); throw new ParseException(errMsg, e.getErrorOffset()); } long timeStamp = date.getTime(); return timeStamp; }
From source file:org.archive.util.ArchiveUtils.java
/** * Parses an ARC-style date. If passed String is < 12 characters in length, * we pad. At a minimum, String should contain a year (>=4 characters). * Parse will also fail if day or month are incompletely specified. Depends * on the above getXXDigitDate methods./*from w ww . j av a 2 s . c o m*/ * @param A 4-17 digit date in ARC style (<code>yyyy</code> to * <code>yyyyMMddHHmmssSSS</code>) formatting. * @return A Date object representing the passed String. * @throws ParseException */ public static Date getDate(String d) throws ParseException { Date date = null; if (d == null) { throw new IllegalArgumentException("Passed date is null"); } switch (d.length()) { case 14: date = ArchiveUtils.parse14DigitDate(d); break; case 17: date = ArchiveUtils.parse17DigitDate(d); break; case 12: date = ArchiveUtils.parse12DigitDate(d); break; case 0: case 1: case 2: case 3: throw new ParseException("Date string must at least contain a" + "year: " + d, d.length()); default: if (!(d.startsWith("19") || d.startsWith("20"))) { throw new ParseException("Unrecognized century: " + d, 0); } if (d.length() < 8 && (d.length() % 2) != 0) { throw new ParseException("Incomplete month/date: " + d, d.length()); } StringBuilder sb = new StringBuilder(d); while (sb.length() < 8) { sb.append("01"); } while (sb.length() < 12) { sb.append("0"); } date = ArchiveUtils.parse12DigitDate(sb.toString()); } return date; }
From source file:org.hypertable.hadoop.mapred.TextTableInputFormat.java
public void parseTimestampInterval(JobConf job) throws ParseException { String str = job.get(TIMESTAMP_INTERVAL); if (str != null) { Date ts;/* w w w .j a v a2 s . c o m*/ long epoch_time; String[] parsedRelop = parseRelopSpec(str, "TIMESTAMP"); if (parsedRelop == null) throw new ParseException("Invalid TIMESTAMP interval: " + str, 0); if (parsedRelop[0] != null && parsedRelop[0].length() > 0) { ts = Time.parse_ts(parsedRelop[0]); epoch_time = ts.getTime() * 1000000; m_base_spec.setStart_time(epoch_time); if (parsedRelop[1].equals("=")) m_base_spec.setEnd_time(epoch_time); } if (parsedRelop[4] != null && parsedRelop[4].length() > 0) { ts = Time.parse_ts(parsedRelop[4]); epoch_time = ts.getTime() * 1000000; m_base_spec.setEnd_time(epoch_time); if (parsedRelop[3].equals("=")) m_base_spec.setStart_time(epoch_time); } } }
From source file:de.codesourcery.utils.xml.XmlHelper.java
public boolean getBooleanAttribute(Element root, String attrName, boolean isRequired) throws ParseException { String sValue = root.getAttribute(attrName); if (!StringUtils.isBlank(sValue)) { if ("1".equalsIgnoreCase(sValue) || "true".equalsIgnoreCase(sValue) || "yes".equalsIgnoreCase(sValue)) { return true; }//w w w . jav a 2 s. c o m return false; } else { if (isRequired) { final String msg = "Tag <" + root.getNodeName() + "> is lacking required boolean attribute " + attrName; log.error("getBooleanAttribute(): " + msg); throw new ParseException(msg, -1); } return false; } }
From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityXmlParser.java
@Override protected ActivityInfo parsePreparedItem(ActivityContext cData) throws ParseException { if (cData == null || cData.getData() == null) { return null; }/*from w w w. j av a 2 s .com*/ ActivityInfo ai = new ActivityInfo(); ActivityField field = null; cData.setActivity(ai); try { String[] savedFormats = null; String[] savedUnits = null; String[] savedLocales = null; // apply fields for parser Object[] values; for (ActivityField aField : fieldList) { values = null; cData.setField(aField); field = aField; List<ActivityFieldLocator> locators = field.getLocators(); if (locators != null) { // need to save format and units specification from config // in case individual entry in activity data overrides it if (ArrayUtils.getLength(savedFormats) < locators.size()) { savedFormats = new String[locators.size()]; savedUnits = new String[locators.size()]; savedLocales = new String[locators.size()]; } values = parseLocatorValues(locators, cData); for (int li = 0; li < locators.size(); li++) { ActivityFieldLocator loc = locators.get(li); savedFormats[li] = loc.getFormat(); savedUnits[li] = loc.getUnits(); savedLocales[li] = loc.getLocale(); if (values[li] == null && (loc.isRequired() || (requireAll && loc.isDefaultRequire()))) { logger().log(OpLevel.WARNING, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "ActivityXmlParser.required.locator.not.found"), loc, field); cData.setActivity(null); return null; } } } applyFieldValue(field, Utils.simplifyValue(values), cData); if (locators != null && savedFormats != null) { for (int li = 0; li < locators.size(); li++) { ActivityFieldLocator loc = locators.get(li); loc.setFormat(savedFormats[li], savedLocales[li]); loc.setUnits(savedUnits[li]); } } } } catch (Exception e) { ParseException pe = new ParseException(StreamsResources.getStringFormatted( StreamsResources.RESOURCE_BUNDLE_NAME, "ActivityParser.parsing.failed", field), 0); pe.initCause(e); throw pe; } return ai; }
From source file:org.opentides.util.StringUtil.java
@Deprecated public static Date convertFlexibleDate(String strDate) throws ParseException { if (StringUtil.isEmpty(strDate)) throw new ParseException("Cannot convert empty string to Date.", 0); String[] formats = { "MM/dd/yyyy", "MM-dd-yyyy", "yyyyMMdd", "yyyy-MM-dd", "MMM dd yyyy", "MMM dd, yyyy", "MMM yyyy", "MM/yyyy", "MM-yyyy", "yyyy" }; return convertFlexibleDate(strDate, formats); }
From source file:org.pentaho.di.core.util.DateDetector.java
/** * //from ww w. j a v a2 s . c o m * @param dateString * date string for parse * @return {@link java.util.Date} converted from dateString by detected format * @throws ParseException * - if we can not detect date format for string or we can not parse date string */ public static Date getDateFromString(String dateString) throws ParseException { String dateFormat = detectDateFormat(dateString); if (dateFormat == null) { throw new ParseException("Unknown date format.", 0); } return getDateFromStringByFormat(dateString, dateFormat); }
From source file:org.ut.biolab.medsavant.client.query.QueryViewController.java
private SearchConditionGroupItem getGroupFromXML(Element rootElement, SearchConditionGroupItem parentGroup) throws ParseException { if (!rootElement.getNodeName().toLowerCase().equals("group")) { DialogUtils.displayError("ERROR: Malformed/Invalid Input file"); }//from w ww . j av a2s. com QueryRelation qr; if (rootElement.getAttribute("queryRelation").equalsIgnoreCase("OR")) { qr = QueryRelation.OR; } else if (rootElement.getAttribute("queryRelation").equalsIgnoreCase("AND")) { qr = QueryRelation.AND; } else { throw new ParseException("Malformed input file contains invalid query relation", 0); } String desc = rootElement.getAttribute("description"); SearchConditionGroupItem scg; if (parentGroup == null) { scg = this.rootGroup; } else { scg = new SearchConditionGroupItem(qr, null, parentGroup); if (desc != null && desc.length() > 0) { scg.setDescription(desc); } } NodeList nl = rootElement.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) n; if (el.getNodeName().equalsIgnoreCase("group")) { SearchConditionGroupItem childGroup = getGroupFromXML(el, scg); scg.addItem(childGroup); } else if (el.getNodeName().equalsIgnoreCase("item")) { SearchConditionItem sci = getItemFromXML(el, scg); //scg.addItem(sci); } else { throw new ParseException("Malformed input file contains invalid XML element", 0); } } } return scg; }
From source file:com.calc.BracerParser.java
/** * Evaluates once parsed math expression with "var" variable included * * @param variableValue User-specified <code>Double</code> value * @return <code>String</code> representation of the result * @throws <code>ParseException</code> if the input expression is not * correct * @since 3.0//from ww w . j a v a2 s . c om */ public String evaluate(double variableValue) throws ParseException { /* check if is there something to evaluate */ if (stackRPN.empty()) { return ""; } /* clean answer stack */ stackAnswer.clear(); /* get the clone of the RPN stack for further evaluating */ @SuppressWarnings("unchecked") Stack<String> stackRPN = (Stack<String>) this.stackRPN.clone(); /* enroll the variable value into expression */ Collections.replaceAll(stackRPN, VARIABLE, Double.toString(variableValue)); /* evaluating the RPN expression */ while (!stackRPN.empty()) { String token = stackRPN.pop(); if (isNumber(token)) { stackAnswer.push(token); } else if (isOperator(token)) { Complex a = complexFormat.parse(stackAnswer.pop()); Complex b = complexFormat.parse(stackAnswer.pop()); boolean aBoolean = a.getReal() == 1.0; boolean bBoolean = b.getReal() == 1.0; switch (token) { case "+": stackAnswer.push(complexFormat.format(b.add(a))); break; case "-": stackAnswer.push(complexFormat.format(b.subtract(a))); break; case "*": stackAnswer.push(complexFormat.format(b.multiply(a))); break; case "/": stackAnswer.push(complexFormat.format(b.divide(a))); break; case "|": stackAnswer.push(String.valueOf(aBoolean || bBoolean ? "1" : "0")); break; case "&": stackAnswer.push(String.valueOf(aBoolean && bBoolean ? "1" : "0")); break; } } else if (isFunction(token)) { Complex a = complexFormat.parse(stackAnswer.pop()); boolean aBoolean = a.getReal() == 1.0; switch (token) { case "fat": stackAnswer.push(complexFormat.format(a.abs())); break; case "fib": stackAnswer.push(complexFormat.format(a.acos())); break; case "arg": stackAnswer.push(complexFormat.format(a.getArgument())); break; case "asin": stackAnswer.push(complexFormat.format(a.asin())); break; case "atan": stackAnswer.push(complexFormat.format(a.atan())); break; case "conj": stackAnswer.push(complexFormat.format(a.conjugate())); break; case "cos": stackAnswer.push(complexFormat.format(a.cos())); break; case "cosh": stackAnswer.push(complexFormat.format(a.cosh())); break; case "exp": stackAnswer.push(complexFormat.format(a.exp())); break; case "imag": stackAnswer.push(complexFormat.format(a.getImaginary())); break; case "log": stackAnswer.push(complexFormat.format(a.log())); break; case "neg": stackAnswer.push(complexFormat.format(a.negate())); break; case "real": stackAnswer.push(complexFormat.format(a.getReal())); break; case "sin": stackAnswer.push(complexFormat.format(a.sin())); break; case "sinh": stackAnswer.push(complexFormat.format(a.sinh())); break; case "sqrt": stackAnswer.push(complexFormat.format(a.sqrt())); break; case "tan": stackAnswer.push(complexFormat.format(a.tan())); break; case "tanh": stackAnswer.push(complexFormat.format(a.tanh())); break; case "pow": Complex b = complexFormat.parse(stackAnswer.pop()); stackAnswer.push(complexFormat.format(b.pow(a))); break; case "not": stackAnswer.push(String.valueOf(!aBoolean ? "1" : "0")); break; } } } if (stackAnswer.size() > 1) { throw new ParseException("Some operator is missing", 0); } return stackAnswer.pop(); }