规范化时间字符串
private final static Pattern PATTERN_TIME = Pattern
.compile("\\d{4}-\\d{1,2}-\\d{1,2}");
private final static Pattern PATTERN_TIME_HALF = Pattern
.compile("\\d{2}-\\d{1,2}-\\d{1,2}");
private final static Pattern PATTERN_Month = Pattern
.compile("\\d{4}-\\d{1,2}");
private final static Pattern PATTERN_YEAR = Pattern.compile("\\d{4}");
private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
protected long formatTime(String value) {
if (value != null && !value.equals("")) {
value = value.replaceAll("\\.|年|月", "-");
Matcher matcher = PATTERN_TIME.matcher(value);
Matcher matcherHalf = PATTERN_TIME_HALF.matcher(value);
Matcher matcherMonth = PATTERN_Month.matcher(value);
Matcher matcherYear = PATTERN_YEAR.matcher(value);
if (matcher.find()) {
value = matcher.group();
} else if (matcherHalf.find()) {
value = "20" + matcherHalf.group();
} else if (matcherMonth.find()) {
value = matcherMonth.group() + "-01";
} else if (matcherYear.find()) {
value = matcherYear.group() + "-01-01";
}
if (value.contains("待定") || value.contains("暂无")
|| value.equals("") || value.equals(" ")
|| value.equals("0") ) {
System.out.println(value);
return 0;
}
value = value.replaceAll(" | ", "");
value = value.replaceAll("\\.|年|月", "-");
value = value.replaceAll("日|秒", " ");
value = value.replaceAll("时|分", ":");
int temn = charCount(value, ":");
if (temn == 0) {
value += " 00:00:00";
} else if (temn == 1) {
value += ":00";
} else if (temn == 2) {
value += "00";
}
try {
return simpleDateFormat.parse(value).getTime();
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
} |