Calculate difference between 2 JAVA dates [closed]

0

I have searched the browser and others and can not find the solution I'm looking for, so I mention it here.

I have two parameters:

Date1 - > It is the date in which a person registers in the company.

Date2 - > It is the date on which the person withdraws or leaves the company.

Well, I want you to calculate the difference between the two, that is, the following:

Date1 - > 02/08/2010

Date2 - > 10/12/2020

I want you to know that the difference between the two is 10 years.

Greetings and thanks in advance.

    
asked by Manuee 22.11.2018 в 15:24
source

1 answer

1

If you only want to calculate the difference between the years you can do it like this:

public static int calcularDiferenciaYears(Date inicio, Date fin) {
   Calendar a = new Calendar.getInstance();
   a.setTime(inicio);
   Calendar b = new Calendar.getInstance();
   b.setTime(fin);
   int diferencia= b.get(YEAR) - a.get(YEAR);
   return diferencia;
}
    
answered by 22.11.2018 / 15:30
source