java通过出生时间计算年龄

2017-08-16  本文已影响0人  ffday

见代码:

   String year = str.split(" ")[0];//birth_date='1900-10-17 00:00:00.0'
   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
   int age = 0;
   try {
      Date birthDay = sdf.parse(year);
      //获取当前系统时间
      Calendar cal = Calendar.getInstance();
      //如果出生日期大于当前时间,则抛出异常
      if (cal.before(birthDay)) {
         throw new IllegalArgumentException(
               "The birthDay is before Now.It's nbelievable!");
      }
      //取出系统当前时间的年、月、日部分
      int yearNow = cal.get(Calendar.YEAR);
      int monthNow = cal.get(Calendar.MONTH);
      int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
      //将日期设置为出生日期
      cal.setTime(birthDay);
      //取出出生日期的年、月、日部分
      int yearBirth = cal.get(Calendar.YEAR);
      int monthBirth = cal.get(Calendar.MONTH);
      int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
      //当前年份与出生年份相减,初步计算年龄
      age = yearNow - yearBirth;
      //当前月份与出生日期的月份相比,如果月份小于出生月份,则年龄上减1,表示不满多少周岁
      if (monthNow <= monthBirth) {
         //如果月份相等,在比较日期,如果当前日,小于出生日,也减1,表示不满多少周岁
         if (monthNow == monthBirth) {
            if (dayOfMonthNow < dayOfMonthBirth) age--;
         } else {
            age--;
         }
      }
      System.out.println("age:" + age);
   } catch (Exception e) {
      e.printStackTrace();
   }
   return age;
}
上一篇下一篇

猜你喜欢

热点阅读