All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Compare Two Date In Java

Last Updated : Mar 11, 2024

How To Compare Two Date In Java

In this article we will show you the solution of how to compare two date in java, comparing dates when working with date and time operations in Java is a regular task. A Java.time a reliable and simple API for handling date and time objects is provided by the LocalDateTime class, which was introduced in Java 8.

In this article, we'll look at utilising Java's LocalDateTime class to compare two dates.

This lesson will walk you through using the java.time to compare two dates.Java's LocalDateTime class. We'll go over how to compare dates step-by-step and give a detailed breakdown of the code.

We'll make use of the java.time module's capabilities to compare two dates.Class LocalDateTime.

We may compare dates based on their year, month, day, hour, minute, and second values thanks to this class, which represents a date-time without a time zone.

Step By Step Guide On How To Compare Two Date In Java :-

import java.time.LocalDateTime;
public class DateComparison {
    public static void main(String[] args) {
        LocalDateTime d1 = LocalDateTime.of(2023, 7, 15, 10, 30);
        LocalDateTime d2 = LocalDateTime.of(2023, 7, 17, 15, 45);
        if (d1.isBefore(d2)) {
            System.out.println("Date 1 is before Date 2");
        } else if (d1.isAfter(d2)) {
            System.out.println("Date 1 is after Date 2");
        } else {
            System.out.println("Date 1 and Date 2 are equal");
        }
    }
}
  1. Importing java.time.LocalDateTime is the first step. Work with dates and timings.
  2. To represent the dates we want to compare, we construct two LocalDateTime objects, d1 and d2, inside the main method.
  3. We initialise the d1 and d2 objects with precise values for the year, month, day, hour, and minute using the LocalDateTime.of() function.
  4. The isBefore() and isAfter() methods of the LocalDateTime class are then used to compare the two dates. These procedures provide back a boolean answer indicating whether one date comes first or second.
  5. If d1 comes first, we will print "Date 1 is before Date 2."
  6. When date 1 is later than date 2, we print "Date 1 is after Date 2."
  7. Since the two dates are equal if neither criterion is met, we print "Date 1 and Date 2 are equal."

Conclusion :-

In this article, we learned how to use the java.time to compare two dates. Java's LocalDateTime class.

We can quickly ascertain the relative ordering of dates by using the isBefore() and isAfter() functions.

A robust collection of classes for managing dates, times, and intervals is available in the java.time package, allowing programmers to carry out difficult operations with ease.

You may give your Java programmes precise and dependable date and time capabilities by learning how to compare dates.

I hope this article on how to compare two date in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪