How do I calculate someone's age in Java?

前端 未结 28 2315
渐次进展
渐次进展 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 02:58

    With Java 8, we can calculate a person age with one line of code:

    public int calCAge(int year, int month,int days){             
        return LocalDate.now().minus(Period.of(year, month, days)).getYear();         
    }
    

提交回复
热议问题