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

时间: 2024-10-07 17:31:44

java 出生日期 计算年龄的相关文章

js/java中计算年龄

js中计算年龄  /**  * 将生日转换成年龄  */  function birthDayToAge(){  var aDate=new Date();  var thisYear = aDate.getFullYear();  var thisMonth = aDate.getMonth()+1;  var thisDay = aDate.getDate();  var currentDate = thisYear+"-"+thisMonth+"-"+this

Mysql 根据出生日期计算年龄

最近因为业务要求需要根据出生日期计算年龄,在网上查了好多的方法,在这里总结一下. 网上的计算方法好多都提到了格里高利历法,特意去查了下资料,普及点知识. 格里高利历是公历的标准名称,是一种源自于西方社会的历法.它先由意大利医生.天文学家.哲学家.年代学家阿洛伊修斯·里利乌斯(Aloysius Lilius,约1519-1576) 与克拉乌(Christophorus Clavius)等学者在儒略历的基础上加以改革,后由教皇格里高利十三世于1582年颁布.而公元即"公历纪元",又称&qu

根据出生日期计算年龄的sql各种数据库写法

ORACLE数据库: FLOOR(MONTHS_BETWEEN(SYSDATE,出生日期)/12) KINGBASE数据库: DATEDIFF(MONTH,出生日期,CURRENT_TIMESTAMP)/12 DB2数据库: TIMESTAMPDIFF(64,CHAR(CURRENT DATE - TIMESTAMP(出生日期)))/12

C#根据出生日期计算年龄

public int CalculateAge(DateTime birthDate, DateTime now)        {            int age = now.Year - birthDate.Year;            if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))            {         

依据出生日期Date 计算年龄

依据出生日期计算年龄 public class DateGetAge { public static int getAge(Date birthDay) throws Exception { Calendar cal = Calendar.getInstance(); if (cal.before(birthDay)) { throw new IllegalArgumentException( "The birthDay is before Now.It's unbelievable!"

根据出生日期Date 计算年龄

根据出生日期计算年龄 public class DateGetAge { public static int getAge(Date birthDay) throws Exception { Calendar cal = Calendar.getInstance(); if (cal.before(birthDay)) { throw new IllegalArgumentException( "The birthDay is before Now.It's unbelievable!"

java实现根据身份证计算年龄的两种方式

第一种(推荐使用): import org.apache.hadoop.hive.ql.exec.UDF; import java.util.Calendar; public class GetAge extends UDF { public String evaluate(String sfzjh){ if(sfzjh == null || "".equals(sfzjh) ){ return "身份证件号有误,无法计算年龄"; } if (sfzjh.lengt

Java 计算年龄

1 public static String getAgeTxt(String birthTime,String beginTime,int level){ 2 if(StringUtils.isBlank(birthTime)||StringUtils.isBlank(beginTime)){ 3 System.out.println("参数中有空值!"); 4 } 5 6 int year = 0,month=0,day=0,hour=0; 7 8 Date birthDate =

java计算年龄

精确到天计算年龄 public static int getAgeByCardId(String card) throws Exception { Integer len = card.length(); if (len == CHINA_ID_MIN_LENGTH) { card = conver15CardTo18(card); } String birthDate = card.substring(6, 14); SimpleDateFormat sdf = new SimpleDateF