/** * @param desc * @return */ public String extractDate(String desc) { String result = desc; Matcher mat = datePattern.matcher(desc); if (mat.find() && (mat.start() == 0)) { HistoricalDate date = new HistoricalDate(mat.group(1)); em.persist(date); ms.setDate(date); result = desc.substring(mat.end()); if (date.getText().matches(UNKNOWN_DATE_RE)) { return result; } String prefix = StringUtils.trimToNull(mat.group(2)); String firstDate = StringUtils.trimToNull(mat.group(3)); String separator = StringUtils.trimToNull(mat.group(4)); String lastDate = StringUtils.trimToNull(mat.group(5)); String suffix = StringUtils.trimToNull(mat.group(6)); if (suffix != null) { if (suffix.matches("c\\.?")) { date.setPrecision(Precision.CENTURY); } else { // Doesn't seem to happen } } if (prefix != null) { if (prefix.equalsIgnoreCase("ca.")) { // mark date as approximate date.setCertainty(Certainty.APPROXIMATE); } else if (prefix.equalsIgnoreCase("possibly")) { // mark date as uncertain date.setCertainty(Certainty.POSSIBLE); } else { // doesen't occur } } setStartDate(firstDate, date); setEndDate(firstDate, lastDate, separator, date); LOGGER.debug(" Date: " + date.getText()); } else { // System.out.println(desc); } return result; }
/** * Helper method to set the start date while parsing a date. * * @param firstDate The string based representation of a date (number only). * @param date The <tt>HistoricalDate</tt> object to be updated. */ private void setStartDate(String firstDate, HistoricalDate date) { // create a date instance try { SimpleDateFormat formatter = new SimpleDateFormat("yyyy"); if (firstDate.length() < 3) { firstDate = firstDate + "00"; } else if (date.getPrecision() == null) { date.setPrecision(Precision.YEAR); } Date d = formatter.parse(firstDate); date.setStartDate(d); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } }