身份证号判断周岁
public Integer checkExpertAge(String idCard) {
Integer personYear = Integer.parseInt(idCard.substring(6, 10));
Integer personMonth = Integer.parseInt(idCard.substring(10, 12));
Integer personDay = Integer.parseInt(idCard.substring(12, 14));
Calendar cal = Calendar.getInstance();
Integer yearNow = cal.get(Calendar.YEAR);
Integer monthNow = cal.get(Calendar.MONTH) + 1;
Integer dayNow = cal.get(Calendar.DATE);
Integer yearMinus = yearNow - personYear;
Integer monthMinus = monthNow - personMonth;
Integer dayMinus = dayNow - personDay;
Integer age = yearMinus;
if (yearMinus == 0) {
age = 0;
} else {
if (monthMinus < 0) {
age = age - 1;
}
if (monthMinus == 0) {
if (dayMinus < 0) {
age = age - 1;
}
}
}return age;
}
用四种方法统计下面字符串"葡萄"出现的次数
public class Test01{
public static void main(String[] args){
String str = "吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮";
String str1 = str.replace("葡萄"," ");
char[] data = str1.toCharArray();
int count = 0;
for(char a : data){
if(a == ' '){
count++;
}
}
System.out.println(count);
int count = 0;
int x = 0;
while(x != -1){
x = str.indexOf("葡萄");
if(x != -1){
count++;
str = str.replaceFirst("葡萄","");
}
}
System.out.println(count);
for(int x = 0; x < str.length() ; x++){
if(str.contains("葡萄")){
str = str.replaceFirst("葡萄","");
}else{
System.out.println(x);
break;
}
}
String[] data = str.split("葡萄");
if(!str.endsWith("葡萄")){
System.out.println(data.length -1);
}else{
System.out.println(data.length);
}
int count = 0;
int x = 0;
while(x != -1){
x = str.indexOf("葡萄");
if(x != -1){
if((x+2) < str.length()){
str = str.substring(x+2);
}
count++;
}
}
System.out.println(count);
String str2 = str.replace("葡萄","");
System.out.println((str.length() - str2.length())/2);
}
}