Java 根据年月日精确计算年龄

时间:2021-02-18 09:47:16
 1 import java.text.SimpleDateFormat;  
 2 import java.util.Calendar;  
 3 import java.util.Date;  
 4   
 5 /** 
 6  * Created by qing on 2017/3/28. 
 7  */  
 8 public class AgeUtils {  
 9     // 根据年月日计算年龄,birthTimeString:"1994-11-14"  
10     public static int getAgeFromBirthTime(String birthTimeString) {  
11         // 先截取到字符串中的年、月、日  
12         String strs[] = birthTimeString.trim().split("-");  
13         int selectYear = Integer.parseInt(strs[0]);  
14         int selectMonth = Integer.parseInt(strs[1]);  
15         int selectDay = Integer.parseInt(strs[2]);  
16         // 得到当前时间的年、月、日  
17         Calendar cal = Calendar.getInstance();  
18         int yearNow = cal.get(Calendar.YEAR);  
19         int monthNow = cal.get(Calendar.MONTH) + 1;  
20         int dayNow = cal.get(Calendar.DATE);  
21   
22         // 用当前年月日减去生日年月日  
23         int yearMinus = yearNow - selectYear;  
24         int monthMinus = monthNow - selectMonth;  
25         int dayMinus = dayNow - selectDay;  
26   
27         int age = yearMinus;// 先大致赋值  
28         if (yearMinus < 0) {// 选了未来的年份  
29             age = 0;  
30         } else if (yearMinus == 0) {// 同年的,要么为1,要么为0  
31             if (monthMinus < 0) {// 选了未来的月份  
32                 age = 0;  
33             } else if (monthMinus == 0) {// 同月份的  
34                 if (dayMinus < 0) {// 选了未来的日期  
35                     age = 0;  
36                 } else if (dayMinus >= 0) {  
37                     age = 1;  
38                 }  
39             } else if (monthMinus > 0) {  
40                 age = 1;  
41             }  
42         } else if (yearMinus > 0) {  
43             if (monthMinus < 0) {// 当前月>生日月  
44             } else if (monthMinus == 0) {// 同月份的,再根据日期计算年龄  
45                 if (dayMinus < 0) {  
46                 } else if (dayMinus >= 0) {  
47                     age = age + 1;  
48                 }  
49             } else if (monthMinus > 0) {  
50                 age = age + 1;  
51             }  
52         }  
53         return age;  
54     }  
55   
56     // 根据时间戳计算年龄  
57     public static int getAgeFromBirthTime(long birthTimeLong) {  
58         Date date = new Date(birthTimeLong * 1000l);  
59         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
60         String birthTimeString = format.format(date);  
61         return getAgeFromBirthTime(birthTimeString);  
62     }  
63 }  

精确到月:

/**
 * Created by admin on 2017/12/15.
 */
public String getAgeFromBirthTime(Date birthDay) {
Calendar cal = Calendar.getInstance();
// 得到当前时间的年、月、日
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH)+1;
int dayNow = cal.get(Calendar.DATE);
// 先截取到传入中的年、月、日
cal.setTime(birthDay);
int selectYear = cal.get(Calendar.YEAR);
int selectMonth = cal.get(Calendar.MONTH) + 1;
int selectDay = cal.get(Calendar.DAY_OF_MONTH);
// 用当前年月日减去生日年月日
int yearMinus = yearNow - selectYear;
int monthMinus = monthNow - selectMonth;
int dayMinus = dayNow - selectDay;
String ageToMonth= "1月";

if (yearMinus > 0){
ageToMonth = String.valueOf(yearMinus)+"岁";
} else if (monthMinus > 0) {
ageToMonth = String.valueOf(monthMinus)+"月";
}
return ageToMonth;
}

  

 

1 public static void main(String[] args) {
2         String dataOfBirth = "2017-10-28";
3         String age = getAgeFromBirthTime(dataOfBirth);
4         System.out.println("age:" + age);
5     }

 

 

鸡肋:

 1 public class TestAge {
 2     public static void main(String[] args) {
 3 //        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 4         try {
 5             //此处是获得的年龄
 6             int age = getAge(parse("2015-12-5 00:00:00"));           //由出生日期获得年龄***
 7             System.out.println(age);
 8         } catch (Exception e) {
 9             e.printStackTrace();
10         }
11     }
12 
13     //出生日期字符串转化成Date对象
14     public static Date parse(String strDate) throws ParseException {
15         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
16         return sdf.parse(strDate);
17     }
18 
19     //由出生日期获得年龄
20     public  static int getAge(Date birthDay) throws Exception {
21         Calendar cal = Calendar.getInstance();
22         if (cal.before(birthDay)) {
23             throw new IllegalArgumentException(
24                     "The birthDay is before Now.It's unbelievable!");
25         }
26         int yearNow = cal.get(Calendar.YEAR);
27         int monthNow = cal.get(Calendar.MONTH);
28         int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
29         cal.setTime(birthDay);
30         int yearBirth = cal.get(Calendar.YEAR);
31         int monthBirth = cal.get(Calendar.MONTH);
32         int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
33         int age = yearNow - yearBirth;
34 
35         if (monthNow <= monthBirth) {
36             if (monthNow == monthBirth) {
37                 if (dayOfMonthNow < dayOfMonthBirth) age--;
38             }else{
39                 age--;
40             }
41         }
42         return age;
43     }