/**
* 根据两个日期字符串,格式为(YYYY-MM-DD),获取年龄,保留两位小数但不四舍五入
* @param birth 出生日期
* @param endDate 截止日期
*
* */
public static String getAge(String birth,String endDate){
if(null!=birth && !"".equals(birth) && null!=endDate && !"".equals(endDate)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date BD=null;
Date ED=null;
try {
BD = sdf.parse(birth);
ED=sdf.parse(endDate);
} catch (ParseException e) {
e.printStackTrace();
}
//获取相减后天数
long days = (ED.getTime()-BD.getTime())/(24*60*60*1000);
double age1=((double)days)/365;
double age =((double)((int)(age1*100)))/100; //保留两位小数但不四舍五入
return age+"";
}else{
return "";
}
}
将(YYYY-MM-DD)日期字符串 转换成天数
public static String getDays(Object date){
if(null!=date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date SD=null;
try {
SD = sdf.parse(date.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获取天数
long days = (SD.getTime())/(24*60*60*1000);
return days+"";
}else{
return "";
}
}