How do I calculate someone's age in Java?

前端 未结 28 2798
渐次进展
渐次进展 2020-11-22 02:20

I want to return an age in years as an int in a Java method. What I have now is the following where getBirthDate() returns a Date object (with the birth date ;-)):



        
28条回答
  •  滥情空心
    2020-11-22 03:07

    I appreciate all correct answers but this is the kotlin answer for the same question

    I hope would be helpful to kotlin developers

    fun calculateAge(birthDate: Date): Int {
            val now = Date()
            val timeBetween = now.getTime() - birthDate.getTime();
            val yearsBetween = timeBetween / 3.15576e+10;
            return Math.floor(yearsBetween).toInt()
        }
    

提交回复
热议问题