Date and Time

Find difference between two dates in java

public final class Period extends Object

A date-based amount of time in the ISO-8601 calendar system, such as ‘2 years, 3 months and 4 days’. This class models a quantity or amount of time in terms of years, months and days. See Duration for the time-based equivalent to this class.

public int getDays()

Gets the amount of days of this period. This returns the days unit.

public int getMonths()

Gets the amount of months of this period. This returns the months unit.

public int getYears()

Gets the amount of years of this period. This returns the years unit.

package com.candidjava.datetime;

import java.time.LocalDate;
import java.time.Period;

public class DifferenceBetweenDates {
	public static void main(String[] args) {
		LocalDate bday = LocalDate.of(1995, 8, 27);
		LocalDate today = LocalDate.of(2030, 8, 27);
		Period difference = Period.between(bday, today);
		int years = difference.getYears();
		int months = difference.getMonths();
		int days = difference.getDays();
		System.out.println("Difference between Dates:");
		System.out.println("Age:" + years + " Years " + months + " Months " + days + " days ");
	}

}

OUTPUT

Difference between Dates:
Age:35 Years 0 Months 0 days