@Override public void insertString(int offs, String val, AttributeSet attr) throws BadLocationException { if (val.length() == 0) super.insertString(offs, val, attr); if (val.length() == 1) { int numChars = 1; if (offs < getLength() - 1) { String nextChar = getText(offs + 1, 1); if (nextChar.equals("-")) { numChars = 2; val = val + "-"; } } String testString = getText(0, offs) + val + getText(offs + numChars, getLength() - (offs + numChars)); try { dateFormatter.parse(testString); } catch (Exception e) { // LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e); return; } super.remove(offs, numChars); super.insertString(offs, val, attr); } else if (val.matches(testRegex)) { super.remove(0, getLength()); super.insertString(getLength(), val, attr); } }
/** * Converts a String in the format of the defined {@link DateUtil#DATE_PATTERN} to a {@link * LocalDate} object. * * <p>Returns null if the String could not be converted. * * @param dateString the date as String * @return the date object or null if it could not be converted */ public static LocalDate parse(String dateString) { try { return DATE_FORMATTER.parse(dateString, LocalDate::from); } catch (DateTimeParseException e) { return null; } }
/** * Determines if the given String contains date time information. Matches date Strings like: * Tuesday, April 5, 2014. */ private boolean containsDayOfWeekAndDate(String line) { try { dayOfWeekDateFormatter.parse(line); return true; } catch (DateTimeParseException ex) { // Ignore } return false; }
@Override protected LocalDateTime fromString(String text) { DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(); if (dateTimeFormatter != null) { try { TemporalAccessor parse = dateTimeFormatter.parse(text); try { return LocalDateTime.from(parse); } catch (DateTimeException e) { CommonUtils.ignoreException(e); } } catch (DateTimeParseException e) { CommonUtils.ignoreException(e); } } return super.fromString(text); }
private LocalDate parseCalendarDate(File file) { try { String html = FileUtils.readFileToString(file); Document doc = Jsoup.parse(html); Element title = doc.select("div.center").get(0); String calTitle = title.text(); Pattern calDatePattern = Pattern.compile( "STATE OF NEW YORK SENATE CALENDAR (?<date>\\w+, \\w+ \\d{1,2}, \\d{4}) .*"); Matcher matcher = calDatePattern.matcher(calTitle); if (matcher.find()) { String dateString = capitalizeAllWords(matcher.group("date")); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy"); return LocalDate.from(dtf.parse(dateString)); } } catch (IOException e) { logger.info("Unable to parse supplemental cal date for file: " + file.getName(), e); } return null; }
private TemporalAccessor stringToTime(String date) { DateTimeFormatter isoFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); return isoFormat.parse(date); }
@Override public Long parse(String text) { if (text == null || text.trim().isEmpty()) return null; return _formatter.parse(text).getLong(getTemporalField()); }