Skip to main content
se añadieron 1995 caracteres en el cuerpo
Origen Enlace

Viendo lo que tu OP requiere este metodo te puede funcionar:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            String CURRDATE = "16-07-1991";
            String EFFDATE = "28-08-2018";
    
            Date startdate = null;
            Date enddate = null;
            try {
                startdate = formatter.parse(CURRDATE);
                enddate = formatter.parse(EFFDATE);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            Calendar startCalendar = new GregorianCalendar();
            startCalendar.setTime(startdate);
    
            Calendar endCalendar = new GregorianCalendar();
            endCalendar.setTime(enddate);
    
            int monthCount = 0;
            int firstDayInFirstMonth = startCalendar.get(Calendar.DAY_OF_MONTH);
            startCalendar.set(Calendar.DAY_OF_MONTH, 1);
            endCalendar.add(Calendar.DAY_OF_YEAR, -firstDayInFirstMonth + 1);
    
            while (!startCalendar.after(endCalendar)) {
                startCalendar.add(Calendar.MONTH, 1);
                ++monthCount;
            }
    
            startCalendar.add(Calendar.MONTH, -1);
            --monthCount;
            int remainingDays = 0;
            while (!startCalendar.after(endCalendar)) {
                startCalendar.add(Calendar.DAY_OF_YEAR, 1);
                ++remainingDays;
            }
    
            startCalendar.add(Calendar.DAY_OF_YEAR, -1);
            --remainingDays;
    
            int lastMonthMaxDays = endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (remainingDays >= lastMonthMaxDays) {
                ++monthCount;
                remainingDays -= lastMonthMaxDays;
            }
    
            int diffMonth = monthCount % 12;
            int diffYear = monthCount / 12;
            int diffDay = remainingDays;
    
            System.out.println(diffYear + " Year(s) and " + diffMonth + " Month(s) and " + diffDay + " Day(s)");

27 Year(s) and 1 Month(s) and 12 Day(s)

Viendo lo que tu OP requiere este metodo te puede funcionar:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            String CURRDATE = "16-07-1991";
            String EFFDATE = "28-08-2018";
    
            Date startdate = null;
            Date enddate = null;
            try {
                startdate = formatter.parse(CURRDATE);
                enddate = formatter.parse(EFFDATE);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            Calendar startCalendar = new GregorianCalendar();
            startCalendar.setTime(startdate);
    
            Calendar endCalendar = new GregorianCalendar();
            endCalendar.setTime(enddate);
    
            int monthCount = 0;
            int firstDayInFirstMonth = startCalendar.get(Calendar.DAY_OF_MONTH);
            startCalendar.set(Calendar.DAY_OF_MONTH, 1);
            endCalendar.add(Calendar.DAY_OF_YEAR, -firstDayInFirstMonth + 1);
    
            while (!startCalendar.after(endCalendar)) {
                startCalendar.add(Calendar.MONTH, 1);
                ++monthCount;
            }
    
            startCalendar.add(Calendar.MONTH, -1);
            --monthCount;
            int remainingDays = 0;
            while (!startCalendar.after(endCalendar)) {
                startCalendar.add(Calendar.DAY_OF_YEAR, 1);
                ++remainingDays;
            }
    
            startCalendar.add(Calendar.DAY_OF_YEAR, -1);
            --remainingDays;
    
            int lastMonthMaxDays = endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            if (remainingDays >= lastMonthMaxDays) {
                ++monthCount;
                remainingDays -= lastMonthMaxDays;
            }
    
            int diffMonth = monthCount % 12;
            int diffYear = monthCount / 12;
            int diffDay = remainingDays;
    
            System.out.println(diffYear + " Year(s) and " + diffMonth + " Month(s) and " + diffDay + " Day(s)");

27 Year(s) and 1 Month(s) and 12 Day(s)

Origen Enlace

Puedes usar la libreria de Joda Time y utilizar el siguente codigo:

import org.joda.time.LocalDate;
import org.joda.time.Years;

LocalDate birthday = new LocalDate(1989, 12, 6);
LocalDate now = new LocalDate(2016, 10, 11);
Years age = Years.yearsBetween(birthday, now);

System.out.println("age: " + age.getYears());  // prints "age: 26"

O tambien si usas Java 8 puedes usar:

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;

LocalDate birthday = LocalDate.of(1989, Month.DECEMBER, 6);
LocalDate now = LocalDate.of(2016, Month.OCTOBER, 11);

// using period
Period period = Period.between(birthday, now);
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println("age: " + period.getYears());  // prints "age: 26"

// using chrono unit
System.out.println("age: " + ChronoUnit.YEARS.between(birthday, now));  // prints "age: 26"