//这些总结的常用方法,直接复制调用即可
//检查输入信息是否为汉字
public static boolean checkname(String name) {
int n = 0;
for(int i = 0; i < name.length(); i++) {
n = (int)name.charAt(i);
if(!(19968 <= n && n <40869)) {
return false;
}
}
return true;
}
//检查字符串是否全是数字
public static boolean checkNum(String str){
Pattern p=Pattern.compile("^-?[0-9]+");
Matcher isNum=p.matcher(str);
if (!isNum.matches()) {//如果格式错误,返回true
return true;
}
return false;
}
// 验证时间字符串格式是否输入正确,yyyy-MM-dd HH:mm:ss
public static boolean DateTime(String timeStr){ //这个空格不能少
String format = “((19|20)[0-9]{2})-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01]) ([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]”;
//"^([1-9]\d{3}-)(([0]{0,1}[1-9]-)|([1][0-2]-))(([0-3]{0,1}[0-9]))$";
、//匹配yyyy-MM-dd
Pattern pattern=Pattern.compile(format);
Matcher matcher=pattern.matcher(timeStr);
if (matcher.matches()) {
pattern=Pattern.compile("(\\d{4})-(\\d+)-(\\d+).*");
matcher=pattern.matcher(timeStr);
if (matcher.matches()) {
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;
}
return false;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200618204104182.png)