How can I find the number of years between two dates ?

How can I find the number of years between two dates ?

Asked on December 28, 2018 in Java.
Add Comment


  • 5 Answer(s)

    Import this code, in your project.

    import java.util.Calendar;
    import java.util.Locale;
    import static java.util.Calendar.*;
    import java.util.Date;
     
    public static int getDiffYears(Date first, Date last) {
        Calendar a = getCalendar(first);
        Calendar b = getCalendar(last);
        int diff = b.get(YEAR) - a.get(YEAR);
        if (a.get(MONTH) > b.get(MONTH) ||
            (a.get(MONTH) == b.get(MONTH) && a.get(DATE) > b.get(DATE))) {
            diff--;
        }
        return diff;
        }
     
    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance(Locale.US);
        cal.setTime(date);
        return cal;
    }
    
    Answered on December 28, 2018.
    Add Comment

    Everything that date related topics in java is comes under joda.time library.

    In this question we have to used the function in joda.time library called  Years.yearsBetween() .

    Answered on December 28, 2018.
    Add Comment

    A function named DAY_OF_YEAR can be used for find the number of years between two dates.

    public static int getDiffYears(Date first, Date last) {
        Calendar a = getCalendar(first);
        Calendar b = getCalendar(last);
        int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR);
        if (a.get(Calendar.DAY_OF_YEAR) > b.get(Calendar.DAY_OF_YEAR)) {
            diff--;
        }
        return diff;
    }
     
    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance(Locale.US);
        cal.setTime(date);
        return cal;
    }
    

    We have to diff the milliseconds of the time and divide it by the number of milliseconds in a whole year. Maintain these things long most of the time but it is not constant, it is depends on the application for the number of years.

    Answered on December 28, 2018.
    Add Comment

    Using Code:
    Here is the complete code to find the number of months and years between two dates in Java using java.util.Calendar class. It uses GregorianCalendar, which we all use on our day to day life. You must remember that month in Calendar starts from zero i.e. the first month, January is equal to zero.

     // Using Calendar - calculating number of months between two dates  
            Calendar birthDay = new GregorianCalendar(1955, Calendar.MAY, 19);
            Calendar today = new GregorianCalendar();
            today.setTime(new Date());
            
            int yearsInBetween = today.get(Calendar.YEAR) 
                                    - birthDay.get(Calendar.YEAR);
            int monthsDiff = today.get(Calendar.MONTH) 
                                    - birthDay.get(Calendar.MONTH);
            long ageInMonths = yearsInBetween*12 + monthsDiff;
            long age = yearsInBetween;
            
            System.out.println("Number of months since James gosling born : "
                    + ageInMonths);
            
            System.out.println("Sir James Gosling's age : " + age);

     

    Answered on January 16, 2019.
    Add Comment

    I apparently can’t comment yet, but I think you can just use the DAY_OF_YEAR to workout if you should adjust the years down one (copied and modified from current best answer)

    public static int getDiffYears(Date first, Date last) {
        Calendar a = getCalendar(first);
        Calendar b = getCalendar(last);
        int diff = b.get(Calendar.YEAR) - a.get(Calendar.YEAR);
        if (a.get(Calendar.DAY_OF_YEAR) > b.get(Calendar.DAY_OF_YEAR)) {
            diff--;
        }
        return diff;
    }
    
    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance(Locale.US);
        cal.setTime(date);
        return cal;
    }
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.