Beginners Programs :

Java Code to Calculate Years Between Two Dates | Java Programs

Java program to find years between two dates – The following java program has been written in along with algorithmic explanation, if you have any doubts regarding that just do leave a comment here.

Java Program Calculate Years Between Dates

Output:

Explanation:

To calculate the exact number of years in between two dates, we will require to store 2 dates in a 2-D array (date[2][3]).

When one dimension is for first date and second date while the other is to store the year, month and day of each date. Along with this, we’ll also have another 1-D array (m) which has the fixed value of number of days in each month.

We first take in values of our date by making use of Scanner class to read it at runtime from the console screen.

In date array, date[0] is the first date while date[1] is the second date. Also for a given date[i], date[i][0] is the year, date[i][1] is the month and date[i][2] is the day.

After reading the first date (date[0]), we check if the entered date is valid or not by calling check_valid_date() method and sending date[0] and array m as parameters.

So now, the execution goes to the check_valid_date() method.

Here, first we check if the entered year is greater than zero and then, if it is leap year by calling the leapyear() method.

A year is said to be a leap year if it is divisible by 4 but, if it is also divisible by 100 under that case, it should be divisible by 400. Only if these conditions satisfy, it can be called a leap year.

Here, if this method returns 1 it’s considered a leap year and if not then it isn’t. If in check_valid_date() method, it is a leap year and if the month i.e., (date[0][1] or here a[1]) is equal to 2 (February) then, we make use of a temp variable and assign it the value 1 else it is zero only.

This is used because in case of leap year February has 29 days while in our mentioned array we have only but 28 so, if this condition is met then, the temp variable with value 1 can be added to array (m[2]) value by making it 29.

After this, we call the monthvalidation() method in the check_valid_date() method. In the monthvalidation() method we send the value of month(a[1]) and day(a[2]) along with array m containing number of days in a particular month.

In this method, first it checks whether the entered month is between 1 to 12 both inclusive and then if the day is lesser than the maximum possible days in that month.

If both these conditions are true the monthvalidation() method returns 1 else it returns 0.

static int monthvalidation(int month,int days,int m) {

 

Only if the monthvalidation() method returns 1 then, the check_valid_date() returns 1. Otherwise in any other scenario it returns 0 only.

If check_valid_date() method returns one then the next date is read and again for the second date (date[1]), the check_valid_date() method sees if we have entered the valid date.

If check_valid_date() method returns zero error message is printed telling that the date is invalid.

After entering the two dates and checking the validity, we see if the first entered date is smaller than the second entered date. If it is greater then, we print the error message and stop the execution else, we proceed to further steps.

Now, if first entered date is smaller than the second entered date then, we again check whether it is leap year or not for safe side and then, we calculate the difference between the two years by subtracting the smaller from the larger and store it in a variable (y).

Now let us consider the scenario where, month of second date is smaller than first(date[1][1]<date[0][1]) then, it means that, the last year is still not completed, under than scenario, we decrement y variable by 1. difference between months will then be calculated as 12-date[0][1]+date[1][1] and will be stored in another variable (m1).

Similar to year we check if day of second date is small than first then, it means that month is not completed so m1 variable is decremented and difference of days between the two variables is calculated as m[date[0][1]]-date[0][2]+date[1][2] and stored in another variable (d).

If day of second is not smaller than first then, the difference is calculated as usual only.

d=date[1][2]-date[0][2];

Similarly, if the month of second date is not smaller than first then, the difference is calculated as usual only.

m1=date[1][1]-date[0][1];

Finally, after getting the exact difference between the two dates in years (y), months (m1) and days (d), we display them in our console screen using println() method.

System.out.println(date[0][2]+”-“+date[0][1]+”-“+date[0][0]+” to “+date[1][2]+”-“+date[1][1]+”-“+date[1][0]);

System.out.println(y+”years “+m1+”months “+d+”days”);