How do I calculate someone's age in Java?

前端 未结 28 2788
渐次进展
渐次进展 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:15

    With the date4j library :

    int age = today.getYear() - birthdate.getYear();
    if(today.getDayOfYear() < birthdate.getDayOfYear()){
      age = age - 1; 
    }
    

提交回复
热议问题