• java 出生日期 计算年龄


     1 public static int getAge(Date birthDay) throws Exception {
     2         Calendar cal = Calendar.getInstance();
     3 
     4         if (cal.before(birthDay)) {
     5             throw new IllegalArgumentException(
     6                 "The birthDay is before Now.It's unbelievable!");
     7         }
     8 
     9         int yearNow = cal.get(Calendar.YEAR);
    10         int monthNow = cal.get(Calendar.MONTH);
    11         int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
    12         cal.setTime(birthDay);
    13 
    14         int yearBirth = cal.get(Calendar.YEAR);
    15         int monthBirth = cal.get(Calendar.MONTH);
    16         int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
    17 
    18         int age = yearNow - yearBirth;
    19 
    20         if (monthNow <= monthBirth) {
    21             if (monthNow == monthBirth) {
    22                 //monthNow==monthBirth
    23                 if (dayOfMonthNow < dayOfMonthBirth) {
    24                     age--;
    25                 } else {
    26                     //do nothing
    27                 }
    28             } else {
    29                 //monthNow>monthBirth
    30                 age--;
    31             }
    32         } else {
    33             //monthNow<monthBirth
    34             //donothing
    35         }
    36 
    37         return age;
    38     }

    转载: http://www.blogjava.net/iamtin/archive/2006/03/10/34608.html

  • 相关阅读:
    杨辉三角1
    岛屿与周长
    什么是计算机语言
    爬虫爬取视图片
    爬虫爬取文字生成词云
    英文词频统计
    回溯法~0-1背包的实现
    java Swing图形化界面
    棋盘覆盖
    敏捷软件开发 10~12章
  • 原文地址:https://www.cnblogs.com/MDK-L/p/4466271.html
Copyright © 2020-2023  润新知