Java 根據年月日精確計算年齡


 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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM