How do I calculate someone's age in Java?

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

    Modern answer and overview

    a) Java-8 (java.time-package)

    LocalDate start = LocalDate.of(1996, 2, 29);
    LocalDate end = LocalDate.of(2014, 2, 28); // use for age-calculation: LocalDate.now()
    long years = ChronoUnit.YEARS.between(start, end);
    System.out.println(years); // 17
    

    Note that the expression LocalDate.now() is implicitly related to the system timezone (which is often overlooked by users). For clarity it is generally better to use the overloaded method now(ZoneId.of("Europe/Paris")) specifying an explicit timezone (here "Europe/Paris" as example). If the system timezone is requested then my personal preference is to write LocalDate.now(ZoneId.systemDefault()) to make the relation to the system timezone clearer. This is more writing effort but makes reading easier.

    b) Joda-Time

    Please note that the proposed and accepted Joda-Time-solution yields a different computation result for the dates shown above (a rare case), namely:

    LocalDate birthdate = new LocalDate(1996, 2, 29);
    LocalDate now = new LocalDate(2014, 2, 28); // test, in real world without args
    Years age = Years.yearsBetween(birthdate, now);
    System.out.println(age.getYears()); // 18
    

    I consider this as a small bug but the Joda-team has a different view on this weird behaviour and does not want to fix it (weird because the day-of-month of end date is smaller than of start date so the year should be one less). See also this closed issue.

    c) java.util.Calendar etc.

    For comparison see the various other answers. I would not recommend using these outdated classes at all because the resulting code is still errorprone in some exotic cases and/or way too complex considering the fact that the original question sounds so simple. In year 2015 we have really better libraries.

    d) About Date4J:

    The proposed solution is simple but will sometimes fail in case of leap years. Just evaluating the day of year is not reliable.

    e) My own library Time4J:

    This works similar to Java-8-solution. Just replace LocalDate by PlainDate and ChronoUnit.YEARS by CalendarUnit.YEARS. However, getting "today" requires an explicit timezone reference.

    PlainDate start = PlainDate.of(1996, 2, 29);
    PlainDate end = PlainDate.of(2014, 2, 28);
    // use for age-calculation (today): 
    // => end = SystemClock.inZonalView(EUROPE.PARIS).today();
    // or in system timezone: end = SystemClock.inLocalView().today();
    long years = CalendarUnit.YEARS.between(start, end);
    System.out.println(years); // 17
    
    0 讨论(0)
  • 2020-11-22 02:52

    Elegant, seemingly correct, timestamp difference based variant of Yaron Ronen solution.

    I am including a unit test to prove when and why it is not correct. It is impossible due (to possibly) different number of leap days (and seconds) in any timestamp difference. The discrepancy should be max +-1 day (and one second) for this algorithm, see test2(), whereas Yaron Ronen solution based on completely constant assumption of timeDiff / MILLI_SECONDS_YEAR can differ 10 days for a 40ty year old, nevertheless this variant is incorrect too.

    It is tricky, because this improved variant, using formula diffAsCalendar.get(Calendar.YEAR) - 1970, returns correct results most of the time, as number of leap years in on average same between two dates.

    /**
     * Compute person's age based on timestamp difference between birth date and given date
     * and prove it is INCORRECT approach.
     */
    public class AgeUsingTimestamps {
    
    public int getAge(Date today, Date dateOfBirth) {
        long diffAsLong = today.getTime() - dateOfBirth.getTime();
        Calendar diffAsCalendar = Calendar.getInstance();
        diffAsCalendar.setTimeInMillis(diffAsLong);
        return diffAsCalendar.get(Calendar.YEAR) - 1970; // base time where timestamp=0, precisely 1/1/1970 00:00:00 
    }
    
        final static DateFormat df = new SimpleDateFormat("dd.MM.yyy HH:mm:ss");
    
        @Test
        public void test1() throws Exception {
            Date dateOfBirth = df.parse("10.1.2000 00:00:00");
            assertEquals(87, getAge(df.parse("08.1.2088 23:59:59"), dateOfBirth));
            assertEquals(87, getAge(df.parse("09.1.2088 23:59:59"), dateOfBirth));
            assertEquals(88, getAge(df.parse("10.1.2088 00:00:01"), dateOfBirth));
        }
    
        @Test
        public void test2() throws Exception {
            // between 2000 and 2021 was 6 leap days
            // but between 1970 (base time) and 1991 there was only 5 leap days
            // therefore age is switched one day earlier
                // See http://www.onlineconversion.com/leapyear.htm
            Date dateOfBirth = df.parse("10.1.2000 00:00:00");
            assertEquals(20, getAge(df.parse("08.1.2021 23:59:59"), dateOfBirth));
            assertEquals(20, getAge(df.parse("09.1.2021 23:59:59"), dateOfBirth)); // ERROR! returns incorrect age=21 here
            assertEquals(21, getAge(df.parse("10.1.2021 00:00:01"), dateOfBirth));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:53

    I simply use the milliseconds in a year constant value to my advantage:

    Date now = new Date();
    long timeBetween = now.getTime() - age.getTime();
    double yearsBetween = timeBetween / 3.15576e+10;
    int age = (int) Math.floor(yearsBetween);
    
    0 讨论(0)
  • 2020-11-22 02:54

    String dateofbirth has the date of birth. and format is whatever (defined in the following line):

    org.joda.time.format.DateTimeFormatter formatter =  org.joda.time.format.DateTimeFormat.forPattern("mm/dd/yyyy");
    

    Here is how to format:

    org.joda.time.DateTime birthdateDate = formatter.parseDateTime(dateofbirth );
    org.joda.time.DateMidnight birthdate = new         org.joda.time.DateMidnight(birthdateDate.getYear(), birthdateDate.getMonthOfYear(), birthdateDate.getDayOfMonth() );
    org.joda.time.DateTime now = new org.joda.time.DateTime();
    org.joda.time.Years age = org.joda.time.Years.yearsBetween(birthdate, now);
    java.lang.String ageStr = java.lang.String.valueOf (age.getYears());
    

    Variable ageStr will have the years.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-22 02:56
    public class CalculateAge { 
    
    private int age;
    
    private void setAge(int age){
    
        this.age=age;
    
    }
    public void calculateAge(Date date){
    
        Calendar calendar=Calendar.getInstance();
    
        Calendar calendarnow=Calendar.getInstance();    
    
        calendarnow.getTimeZone();
    
        calendar.setTime(date);
    
        int getmonth= calendar.get(calendar.MONTH);
    
        int getyears= calendar.get(calendar.YEAR);
    
        int currentmonth= calendarnow.get(calendarnow.MONTH);
    
        int currentyear= calendarnow.get(calendarnow.YEAR);
    
        int age = ((currentyear*12+currentmonth)-(getyears*12+getmonth))/12;
    
        setAge(age);
    }
    public int getAge(){
    
        return this.age;
    
    }
    
    0 讨论(0)
提交回复
热议问题