public static List run(String text) {
String dateStr = text.replaceAll("r?n", " ");
List dates = new ArrayList();
try {
Pattern p = Pattern.compile("(\\d{1,4}[-|\\/|年|\\.|\\s]?(0[1-9]|1[1-2])[-|\\/|月|\\.|\\s]?(0[1-9]|[1-2]\\d|3[0-1]))", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
Matcher matcher = p.matcher(dateStr);
while (matcher.find()) {
String temp = matcher.group();
dates.add(temp);
}
} catch (Exception e) {
}
return dates;
}
只能提取同時具有年月日的日期,但可以匹配多種日期的連接方式,可以根據需要進行更改。提取之后可以通過以下方法將時間字符串轉換成標准格式。代碼如下
public static List getDates(List list){
List dates = new ArrayList();
for (String date : list) {
String[] strs = date.split("[-|\\/|年|月|\\.|\\s]");
String time = "";
for (String s : strs) {
time += s;
}
dates.add(time);
}
return dates;
}