Я бы использовал синтаксический анализ Java DateFormat
. Это позволяет вам проходить через строку, используя объект ParsePosition
.
DateFormat format = new SimpleDateFormat("yyyymmddhhmmss");
ParsePosition pp = new ParsePosition(0);
for (int c = 0; c < text.length(); c++) {
pp.setIndex(c); pp.setErrorIndex(-1);
Date d = format.parse(text, pp);
if (d != null) {
// Handle parsed date
// Only advance past the last parsed date if there was no error
if (pp.getErrorIndex() < 0) c = pp.getIndex() - 1;
}
}