How do I calculate the number of years difference between 2 dates?

前端 未结 4 811
一向
一向 2020-12-15 23:15

How do I calculate the number of years difference between 2 calendars in Java?

相关标签:
4条回答
  • 2020-12-15 23:36

    First you need to determine which one is older than the other. Then make use of a while loop wherein you test if the older one isn't after() the newer one. Invoke Calendar#add() with one (1) Calendar.YEAR on the older one to add the years. Keep a counter to count the years.

    Kickoff example:

    Calendar myBirthDate = Calendar.getInstance();
    myBirthDate.clear();
    myBirthDate.set(1978, 3 - 1, 26);
    Calendar now = Calendar.getInstance();
    Calendar clone = (Calendar) myBirthDate.clone(); // Otherwise changes are been reflected.
    int years = -1;
    while (!clone.after(now)) {
        clone.add(Calendar.YEAR, 1);
        years++;
    }
    System.out.println(years); // 32
    

    That said, the Date and Calendar API's in Java SE are actually epic failures. There's a new Date API in planning for upcoming Java 8, the JSR-310 which is much similar to Joda-Time. As far now you may want to consider Joda-Time since it really eases Date/Time calculations/modifications like this. Here's an example using Joda-Time:

    DateTime myBirthDate = new DateTime(1978, 3, 26, 0, 0, 0, 0);
    DateTime now = new DateTime();
    Period period = new Period(myBirthDate, now);
    int years = period.getYears();
    System.out.println(years); // 32
    

    Much more clear and concise, isn't it?

    0 讨论(0)
  • 2020-12-15 23:36
    double diff = d2.getTime() - d1.getTime();
    double d = 1000 * 60 * 60 * 24 * 365;
    return (int) Math.round(diff / d);
    

    (java.lang.Math)

    0 讨论(0)
  • 2020-12-15 23:41

    Or you could just use Calendar.getTimeInMillis():

    long msYear = 1000L * 60 * 60 * 24 * 365;
    long msDiff = c2.getTimeInMillis() - c1.getTimeInMillis();
    System.out.println("Years diff: " + String.valueOf(msDiff / msYear));
    

    Edit This ignores leap years. But if you are looking for an integer representation of the number of years the leap years are irrelevant for periods shorter than half a millennium or so.

    This is plenty of exactness for calculating somebody's age, for example. But not accurate enough to tell their birthday.

    If you need your year difference more exact than to three significant digits within a human lifespan (approximately 80.00 years vs 80.05), you should use the "official" solution.

    If not, there's no particular reason to put in extra effort for a degree of precision you are not going to use.

    0 讨论(0)
  • 2020-12-15 23:45
    Calendar dobDate; // Set this to date to check
    Calendar today = Calendar.getInstance();
    int curYear = today.get(Calendar.YEAR);
    int curMonth = today.get(Calendar.MONTH);
    int curDay = today.get(Calendar.DAY_OF_MONTH);
    
    int year = dobDate.get(Calendar.YEAR);
    int month = dobDate.get(Calendar.MONTH);
    int day = dobDate.get(Calendar.DAY_OF_MONTH);
    
    int age = curYear - year;
    if (curMonth < month || (month == curMonth && curDay < day)) {
        age--;
    }
    

    This avoids looping and should be accurate to the day.

    0 讨论(0)
提交回复
热议问题