根据日期来判断年龄

  

public static String getAge(String str) throws Exception{
ArrayList<Integer> bir = new ArrayList<Integer>();
int i = 0;
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int date = c.get(Calendar.DATE);
int age = 0;
//System.out.println(year+" "+month+" "+date+"");
try {
str = str.trim();
while(str.trim().charAt(0) == '-'){
str = str.substring(1);
}
String temp[] = str.trim().split("-");
if(temp.length == 3){
for(;i < 3; i++){
bir.add(Integer.parseInt(temp[i]));
}
}
if(year - bir.get(0) > 0){
age = year - bir.get(0) - 1;
if(month >= bir.get(1) ){
if(date >= bir.get(2)){
age+=1;
}
}
}
}catch (Exception e){
System.out.println("无法解析为年月日的结构");
throw e;
}
return age+" ";

}

posted @ 2018-07-03 13:58  丶懿  阅读(145)  评论(0编辑  收藏  举报