How do I calculate someone's age in Java?

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

    Easiest way without any libraries:

        long today = new Date().getTime();
        long diff = today - birth;
        long age = diff / DateUtils.YEAR_IN_MILLIS;
    

提交回复
热议问题