Calculate age from BirthDate

后端 未结 17 1361
攒了一身酷
攒了一身酷 2021-02-07 08:33

I have DatePicker Dialog, When I select date at that time I want to calculate age it\'s working but when I select date of current year at that time it showing the -1 age instead

相关标签:
17条回答
  • 2021-02-07 08:34
    public static String calculateAge(String strDate) {
    
        int years = 0;
        int months = 0;
        int days = 0;
    
        try {
            long timeInMillis = Long.parseLong(strDate);
            Date birthDate = new Date(timeInMillis);
    
    
            //create calendar object for birth day
            Calendar birthDay = Calendar.getInstance();
            birthDay.setTimeInMillis(birthDate.getTime());
            //create calendar object for current day
            long currentTime = System.currentTimeMillis();
            Calendar now = Calendar.getInstance();
            now.setTimeInMillis(currentTime);
            //Get difference between years
            years = now.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
            int currMonth = now.get(Calendar.MONTH) + 1;
            int birthMonth = birthDay.get(Calendar.MONTH) + 1;
    
            //Get difference between months
            months = currMonth - birthMonth;
    
    
            //if month difference is in negative then reduce years by one and calculate the number of months.
            if (months < 0) {
                years--;
                months = 12 - birthMonth + currMonth;
                if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE))
                    months--;
            } else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
                years--;
                months = 11;
            }
            //Calculate the days
            if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE))
                days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE);
            else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
                int today = now.get(Calendar.DAY_OF_MONTH);
                now.add(Calendar.MONTH, -1);
                days = now.getActualMaximum(Calendar.DAY_OF_MONTH) - birthDay.get(Calendar.DAY_OF_MONTH) + today;
            } else {
                days = 0;
                if (months == 12) {
                    years++;
                    months = 0;
                }
            }
            //adarsh
            if (currMonth > birthMonth) {
                if (birthDay.get(Calendar.DATE) > now.get(Calendar.DATE)) {
                    months = months - 1;
                }
            }//---------------------------------
    
        } catch (Exception e) {
            e.printStackTrace();
        }
        //Create new Age object
        return years + " Y " + months + " M " + days + " days";
    }
    
    0 讨论(0)
  • 2021-02-07 08:35

    This is how I implement in my source code, I tested. Hope that it is useful :

    public static int getAge(String dateTime, String currentFormat) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(currentFormat);
    
        try {
            Date date = dateFormat.parse(dateTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
    
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
    
    
            Date currentDate = new Date();
            Calendar currentCalendar = Calendar.getInstance();
            currentCalendar.setTime(currentDate);
    
            int currentYear = currentCalendar.get(Calendar.YEAR);
            int currentMonth = currentCalendar.get(Calendar.MONTH);
            int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
    
            int deltaYear = currentYear - year;
            int deltaMonth = currentMonth - month;
            int deltaDay = currentDay - day;
    
            if (deltaYear > 0) {
                if (deltaMonth < 0) {
                    deltaYear --;
                } else if (deltaDay < 0){
                    deltaYear --;
                }
    
                return deltaYear;
            }
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-02-07 08:37
     private boolean getAge(int year, int month, int day) {
        try {
            Calendar dob = Calendar.getInstance();
            Calendar today = Calendar.getInstance();
            dob.set(year, month, day);
            int monthToday = today.get(Calendar.MONTH) + 1;
            int monthDOB = dob.get(Calendar.MONTH)+1;
            int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
            if (age > 18) {
                return true;
            } else if (age == 18) {
                if (monthDOB > monthToday) {
                    return true;
                } else if (monthDOB == monthToday) {
                    int todayDate = today.get(Calendar.DAY_OF_MONTH);
                    int dobDate = dob.get(Calendar.DAY_OF_MONTH);
                    if (dobDate <= todayDate) { // should be less then
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    0 讨论(0)
  • 2021-02-07 08:38
    private int getAge(String dobString){
    
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        try {
            date = sdf.parse(dobString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    
        if(date == null) return 0;
    
        Calendar dob = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
    
        dob.setTime(date);
    
        int year = dob.get(Calendar.YEAR);
        int month = dob.get(Calendar.MONTH);
        int day = dob.get(Calendar.DAY_OF_MONTH);
    
        dob.set(year, month+1, day);
    
        int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
    
        if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
            age--;
        }
    
    
    
        return age;
    }
    
    0 讨论(0)
  • 2021-02-07 08:39

    Here is a Kotlin extension of the Date class returning the age corresponding to a Date object

    val Date.age: Int
    get() {
        val calendar = Calendar.getInstance()
        calendar.time = Date(time - Date().time)
        return 1970 - (calendar.get(Calendar.YEAR) + 1)
    }
    

    It is compatible for all Android versions. If you wonder what '1970' is, that's the Unix Epoch. The timestamp is 0 on January 1, 1970.

    0 讨论(0)
  • 2021-02-07 08:44

    Here is my solution in Kotlin:

    import java.time.LocalDateTime
    
    fun getAge(birthYear: Int, birthMonth: Int, birthDay: Int): Int {
        var age: Int = LocalDateTime.now().year - birthYear
        if (birthMonth > LocalDateTime.now().monthValue || birthMonth == LocalDateTime.now().monthValue && birthDay > LocalDateTime.now().dayOfMonth) { age-- }
        if (age < 0) { age = 0 }
        return age
    }
    
    0 讨论(0)
提交回复
热议问题