Java Code To Find Difference Between Two Dates In Years Months And Days
Chapter:
Miscellaneous
Last Updated:
11-08-2023 16:39:13 UTC
Program:
/* ............... START ............... */
import java.time.LocalDate;
import java.time.Period;
public class DateDifferenceCalculator {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(2020, 3, 15);
LocalDate endDate = LocalDate.of(2023, 8, 11);
Period period = Period.between(startDate, endDate);
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
System.out.println("Difference: " + years + " years, " + months + " months, " + days + " days");
}
}
/* ............... END ............... */
Output
Difference: 3 years, 5 months, 27 days
Notes:
-
Replace the startDate and endDate values with the dates you want to calculate the difference between. The code uses the Period class from the java.time package to calculate the difference in years, months, and days between the two dates.
Tags
Java Code To Find Difference Between Two Dates In Years Months And Days #Write a Java program to calculate the difference between two dates in days, months, and years #Java difference between two dates in days