public judge(){
System.out.println("请输入参数");
}
public judge(String s){
if(s.length()<=17){
System.out.println("号码位数错误");
}else {
Pattern p = Pattern.compile("\\d{18,19}");
String year=s.substring(6,9);
String month=s.substring(10,11);
String day=s.substring(13,14);
if(new Integer(year).intValue()>2022){
System.out.println("身份证号错误");
}
if(new Integer(month).intValue()>12){
System.out.println("身份证号错误");
}
if(new Integer(day).intValue()>31){
System.out.println("身份证号错误");
}
Matcher m = p.matcher(s);
t = m.matches();
}
}
public boolean method(){
return t;
}
}
public class main {
public static void main(String[] args) {
judge j=new judge("111111202302280000");
System.out.println(j.method());
}
}
输出为true。
我不太清楚为什么在judge构造方法中那三个if不会执行,求解答,谢谢
s.substring(6,10)
额.你DEBUG看下那三个值就知道了.你这JDK有点老了啊
截取错了,按照你的代码截取的结果是
year = 202
month = 0
day = 8
正确的应该是
String year=s.substring(6,10);
String month=s.substring(10,12);
String day=s.substring(12,14);
substring(beginIndex, endIndex)
返回此字符串的子字符串。子字符串从指定的beginIndex
开始,并延伸到索引endIndex-1
处的字符。
例如
"hamburger".substring(4, 8) //返回"urge"
"smiles".substring(1, 5) // 返回"mile"