文件名称xxxxx-xxxxx-20211019000000-xxxxxxxx
正则表达式: (202\d\d{0,10})
文件名称xxxx-xxxx-xxx2021-10-19-000000
正则表达式: (202\d\d{0,13})
工具类:
public static Date matchDate(String fileName, String regex) throws Exception {
Pattern pattern = Pattern.compile(regex);
Matcher funcMatcher = pattern.matcher(fileName);
String time = "no match";
if (funcMatcher.find()) {
time = funcMatcher.group(1);
time = time.replaceAll("_", "");
if (time.length() != 14) {
for (int i = time.length(); i < 14; i++) {
time += "0";
}
}
}
Date ret = new SimpleDateFormat("yyyyMMddHHmmss").parse(time);
return ret;
}