How To Find Difference Between Two Dates In Java

Chapter: Date and Time Last Updated: 01-04-2023 14:17:35 UTC

Program:

            /* ............... START ............... */
                import java.time.LocalDate;
import java.time.Period;

public class DateDifferenceExample {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2022, 1, 1);
        LocalDate endDate = LocalDate.of(2022, 12, 31);

        Period period = Period.between(startDate, endDate);
        int years = period.getYears();
        int months = period.getMonths();
        int days = period.getDays();

        System.out.println("Difference between " + startDate + " and " + endDate + " is "
                + years + " years, " + months + " months, and " + days + " days.");
    }
}

                /* ............... END ............... */
        

Output

Difference between 2022-01-01 and 2022-12-31 is 0 years, 11 months, and 30 days.


/*
In this example, the start date is January 1, 2022, and the end date is December 31, 2022.
 The program calculates the difference between these two dates and outputs the result as 0 years, 
11 months, and 30 days. This means that the two dates are almost a year apart (11 months and 30 days).
*/

Notes:

  • The program is designed to demonstrate how to calculate the difference between two dates in Java.
  • First, the program imports the necessary classes from the java.time package, which was introduced in Java 8 to provide a more modern date and time API.
  • Next, the program creates two LocalDate objects named startDate and endDate, which represent the start and end dates, respectively. In this example, the start date is January 1, 2022, and the end date is December 31, 2022.
  • Then, the program uses the Period.between() method to calculate the difference between the two dates in years, months, and days. The result is stored in a Period object named period.
  • Finally, the program extracts the individual year, month, and day components from the Period object using its respective methods getYears(), getMonths(), and getDays(). The program then prints the result to the console, which shows that the difference between the two dates is 0 years, 11 months, and 30 days.
  • Overall, the program demonstrates how to use the java.time package in Java to perform date-related operations such as finding the difference between two dates.

Tags

How to find difference between two dates in Java # Java difference between two dates in days # Java difference between two dates in months

Similar Programs Chapter Last Updated
Java Program to Calculate Time Difference In Hours Date and Time 20-09-2023
Java Code To Calculate Time Difference In Milliseconds Date and Time 20-09-2023
Java Program To Calculate Time Difference In Seconds Date and Time 20-09-2023
How to calculate time difference between two time zones in Java Date and Time 20-09-2023
Java Date And Time API To Create A Custom Calendar Date and Time 20-09-2023
How To Format Date And Time In Java Date and Time 16-09-2023
How Do You Get The First Day Of The Week In Java Date and Time 15-09-2023
Java Code To Change Date Format Date and Time 15-08-2023
Java Program To Find Day Of The Week For A Given Date Date and Time 10-08-2023
Java code to check if date is weekend Date and Time 29-06-2023
Java program to find number of days between two dates Date and Time 29-06-2023
Java Program To Get The Current Time In Different Time Zones Date and Time 22-06-2023
How To Add Working Days To A Date In Java Date and Time 22-06-2023
Java Program To Add A Specified Number Of Days To A Given Date Date and Time 22-06-2023
Write A Program To Check Leap Year In Java Date and Time 22-06-2023
Write A Program To Display Current Date And Time In Java Date and Time 22-06-2023
Java Date Format AM PM Date and Time 14-05-2023
Java Add Months To Date Date and Time 14-05-2023
Add Days To Date In Java example Date and Time 14-05-2023
Java Program To Calculate Days Between Two Dates Date and Time 16-04-2023
Java Program To Add Days To Date Date and Time 16-04-2023
Add Days To Date Java Example Date and Time 08-08-2021
Time Difference Between Two Timestamps In Java Date and Time 22-09-2018

1