/**
* @function getAge * @param birthday:用户孩子的生日 * @description 获取用户孩子的年龄(含年月日) * @return DateVO:用户年龄 * @author maohuidong * @date 2017-05-04 */ public static DateVO getAge(String birthday){ DateVO dateVO = new DateVO(); Date birthDate = null; try { birthDate = new SimpleDateFormat("yyyy-MM-dd").parse(birthday); } catch (ParseException e) { e.printStackTrace(); } //生日这天的日历 Calendar birthCalendar = Calendar.getInstance(); birthCalendar.setTime(birthDate); //今天的日历 Calendar currentCalendar = Calendar.getInstance(); //计算年龄(含年月日) Integer year = currentCalendar.get(Calendar.YEAR) - birthCalendar.get(Calendar.YEAR); Integer month = currentCalendar.get(Calendar.MONTH) - birthCalendar.get(Calendar.MONTH); Integer day = currentCalendar.get(Calendar.DAY_OF_MONTH) - birthCalendar.get(Calendar.DAY_OF_MONTH); if (day < 0) { month -= 1; currentCalendar.add(Calendar.MONTH, -1); day = day + currentCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); } if (month < 0) { month = (month + 12) % 12; year--; } dateVO.setYear(year); dateVO.setMonth(month); dateVO.setDay(day); return dateVO; }