判断日期yyyymmdd
程序员文章站
2022-05-17 21:17:36
...
转载 https://www.cnblogs.com/xiaostudy/p/12566327.html
/**
* yyyyMMdd
*/
String DATA_PATTERN_REG = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|"+
"((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|"+
"((0[48]|[2468][048]|[3579][26])00))0229)$" ;
/**
* 判断是不是 yyyyMMdd 格式
*
* @param date
* @return
*/
public static boolean isYyyyMMdd(String date) {
if (StringUtils.isBlank(date)) {
return false;
}
Pattern pattern = Pattern.compile(DATA_PATTERN_REG);
Matcher matcher = pattern.matcher(date);
boolean matches = matcher.matches();
if(matches){
return true ;
}else {
return isRqFormat(date);
}
}
/**
* 日期分隔符,处理特殊字符
*
* @param splitter
* @return
*/
private static String dealWithSplitter(char splitter) {
String strSplitter = String.valueOf(splitter);
String[] str = {".", "+", "*", "|", "?"};
if (Arrays.asList(str).contains(strSplitter)) {
strSplitter = "[" + strSplitter + "]";
} else if ("^".equals(strSplitter)) {
strSplitter = "\\^";
} else if ("(".equals(strSplitter)) {
strSplitter = "\\(";
} else if (")".equals(strSplitter)) {
strSplitter = "\\)";
} else if ("{".equals(strSplitter)) {
strSplitter = "\\{";
} else if ("}".equals(strSplitter)) {
strSplitter = "\\}";
} else if ("[".equals(strSplitter)) {
strSplitter = "\\[";
} else if ("]".equals(strSplitter)) {
strSplitter = "\\]";
}
return strSplitter;
}
/***
* 判断字符串是否是yyyyMMdd、yyyyMdd、yyyyMMd、yyyyMd格式,分隔符为splitter参数
* 例如:yyyy-MM-dd、yyyy.MM.dd
*
* @param mes
* @param splitter
* @return
*/
public static boolean isRqFormat(String mes, char splitter){
if (null == mes || 0 == mes.length()) {
return false;
}
String strSplitter = dealWithSplitter(splitter);
// 1000年——2020
// String format = "(1[0-9]{3}|20([0-1]{1}[0-9]{1}|20))" + strSplitter + "([1-9]|0[1-9]|1[012])" + strSplitter + "([1-9]|0[1-9]|[12][0-9]|3[01])";
// 0000年——9999年
String format = "([0-9]{4})" + strSplitter + "([1-9]|0[1-9]|1[012])" + strSplitter + "([1-9]|0[1-9]|[12][0-9]|3[01])";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(mes);
if (!matcher.matches()) {
return false;
}
pattern = Pattern.compile("(\\d{4})" + strSplitter + "(\\d{1,2})" + strSplitter + "(\\d{1,2})");
matcher = pattern.matcher(mes);
if (!matcher.matches()) {
return false;
}
int y = Integer.valueOf(matcher.group(1));
int m = Integer.valueOf(matcher.group(2));
int d = Integer.valueOf(matcher.group(3));
if (d > 28) {
Calendar c = Calendar.getInstance();
c.set(y, m-1, 1);
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
return lastDay >= d;
}
return true;
}
/***
* 判断字符串是否是yyyyMMdd、yyyyMdd、yyyyMMd、yyyyMd格式
*
* @param mes
* @return
*/
public static boolean isRqFormat(String mes){
if (null == mes || 0 == mes.length()) {
return false;
}
// 1000年——2020
// String format = "(1[0-9]{3}|20([0-1]{1}[0-9]{1}|20))([1-9]|0[1-9]|1[012])([1-9]|0[1-9]|[12][0-9]|3[01])";
// 0000年——9999年
String format = "([0-9]{4})([1-9]|0[1-9]|1[012])([1-9]|0[1-9]|[12][0-9]|3[01])";
Pattern pattern = Pattern.compile(format);
Matcher matcher = pattern.matcher(mes);
if (!matcher.matches()) {
return false;
}
pattern = Pattern.compile("(\\d{4})(\\d{1,2})(\\d{1,2})");
matcher = pattern.matcher(mes);
if (!matcher.matches()) {
return false;
}
int y = Integer.valueOf(matcher.group(1));
int m = Integer.valueOf(matcher.group(2));
int d = Integer.valueOf(matcher.group(3));
if (d > 28) {
Calendar c = Calendar.getInstance();
c.set(y, m-1, 1);
int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
return lastDay >= d;
}
return true;
}
上一篇: 360浏览器怎么看3个月以前的浏览记录?
下一篇: visio怎么画紫色的卡通葡萄?