All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Difference Between Two Dates

Last Updated : Aug 18, 2023

Java Difference Between Two Dates

In this article we will show you the solution of java difference between two dates, calculating the difference between two dates in Java is a typical task that is often used in exclusive packages. Through the java.Time package, which become first brought in Java 8, Java gives a effective library to manipulate date and time manipulations.

Getting the LocalDate objects that represent the two dates is the first step in computing their difference.

Then, we can calculate dates using the various methods included in the java.time package.

Utilising the Period class, which symbolises a period of time in terms of years, months, & days, is one strategy.

The Period.between() method, which returns the interval between the two dates, can be used to determine the difference between two LocalDate objects.

Now move to the concept of java difference between two dates.

Step By Step Guide On Java Difference Between Two Dates :-

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class DateDifferenceCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first date (YYYY-MM-DD): ");
        String date1Input = scanner.nextLine();
        LocalDate date1 = LocalDate.parse(date1Input);
        System.out.print("Enter the second date (YYYY-MM-DD): ");
        String date2Input = scanner.nextLine();
        LocalDate date2 = LocalDate.parse(date2Input);
        Period periodDifference = Period.between(date1, date2);
        int yearsDifference = periodDifference.getYears();
        int monthsDifference = periodDifference.getMonths();
        int daysDifference = periodDifference.getDays();
        long daysBetween = ChronoUnit.DAYS.between(date1, date2);
        System.out.println("\nDifference using Period class:");
        System.out.println("Years: " + yearsDifference);
        System.out.println("Months: " + monthsDifference);
        System.out.println("Days: " + daysDifference);
        System.out.println("\nDifference using ChronoUnit class:");
        System.out.println("Total days: " + daysBetween);
        scanner.close();
    }
}
  1. To work with dates and time, we import the required classes from the java.time package.
  2. Period, LocalDate, and ChronoUnit. The terms LocalDate and Period both represent dates without a time component, while ChronoUnit offers a variety of units to measure the interval between dates.
  3. To read user input, a Scanner object is created.
  4. We ask them to enter the first date in the format "YYYY-MM-DD" and then read the data using the Scanner object.
  5. After input parsing, date1 is created as a LocalDate object.
  6. In order to read the input similarly, we ask the user to provide the second date in the same way.
  7. Another LocalDate object called date2 is created when the second date is parsed.
  8. Using the Period class and date1 and date2 as inputs, we call the between function to get the difference between the two dates.
  9. This function generates a Period object that represents the years, months, and days between the two dates.
  10. We take the Period object's years, months, and days as discrete components of the difference and store them in distinct integer variables called yearsDifference, monthsDifference, and daysDifference.
  11. We identify the difference between the two dates in total days via the ChronoUnit.DAYS.between() method, that accepts date1 as well as date2 as arguments. The daysBetween variable keeps its result.
  12. Using the Period and ChronoUnit classes, we show the user the results and the differences between the dates.
  13. In order to free up resources, we finally close the Scanner object.

Conclusion :-

As a result, we have successfully learned how to use Java to distinguish between two dates.

We also discovered that the java.time package, which is accessible starting with Java 8, offers strong time and date manipulation capabilities.

I hope this article on java difference between two dates helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪