代码:
public static void main(String[] args) {
//正则判断日期格式^(([1-9]\d{3})|(0\d{2}[1-9]))(0[1-9]|1[0-2])$
Map<String, Object> susMap = new HashMap<>();
Map<String, Object> map = new HashMap<String, Object>();
String susMonth ="20201-10";
String susEndMonth ="2020-112";
String regex = "^(([1-9]\\d{3})|(0\\d{2}[1-9]))(0[1-9]|1[0-2])$";
Pattern pattern = Pattern.compile(regex);
Matcher m1 = pattern.matcher(susMonth);
Matcher m2 = pattern.matcher(susEndMonth);
if (!"-".equals(susMonth) && !"—".equals(susMonth)) {
if (!m1.matches()) {
map.put("type", "1");
map.put("info", "暂停判定数据导入失败:暂停账期日期格式不符合要求");
System.out.println("m1错误");
}
}
if (!"-".equals(susEndMonth) && !"—".equals(susEndMonth)) {
if (!m2.matches()) {
map.put("type", "1");
map.put("info", "暂停判定数据导入失败:暂停失效账期日期格式不符合要求");
System.out.println("m2错误");
}
}
}
输出结果: