package com.lyms.etl.util;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
public static Integer getAge(Date birth) {
if(null==birth){
return null;
}
Calendar calendar= Calendar.getInstance();
Date date=new Date();
calendar.setTime(date);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date);
c2.setTime(birth);
int year1 = c1.get(Calendar.YEAR);
int year2 = c2.get(Calendar.YEAR);
int mouth1 = c1.get(Calendar.MONTH);//0当前
int mouth2 = c2.get(Calendar.MONTH);//4
int i=0;
/* if(mouth2-mouth1>=0){
int days1 = c1.get(Calendar.DAY_OF_MONTH);//当前10
int days2 = c2.get(Calendar.DAY_OF_MONTH);//1
if(days1<days2){
i=i-1;
}else if(days1>days2){
}
}else if(mouth1-mouth2<0){
i=i-1;
}*/
if(mouth2==mouth1){
int days1 = c1.get(Calendar.DAY_OF_MONTH);//当前10
int days2 = c2.get(Calendar.DAY_OF_MONTH);//1
if(days1<days2){
i=i-1;
}else if(days1>days2){
// i=i-1;
}
}else if(mouth2>mouth1){
i=i-1;
}
// if (birth == null) {
// return null;
// }
// Date now = new Date();
// long m = now.getTime() - birth.getTime();
// Long age = m / 31536000000L;
// return age.intValue();
return (Math.abs(year1 - year2)+i);
}
}