All TalkersCode Topics

Follow TalkersCode On Social Media

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

Date Comparison In Java

Last Updated : Mar 11, 2024

Date Comparison In Java

In this article we will show you the solution of date comparison in java, Programmers frequently need to compare dates, particularly when working with time-sensitive processes or analysing temporal data.

There are numerous ways to compare dates in Java and figure out how they relate to one another.

This lesson attempts to give a general overview of the many methods for date comparison in Java, as well as their applications and advantages.

In this article, we'll look at various Java date comparison methods. We will discuss features offered by the more recent java.time package, introduced in Java 8, as well as the java.util.Date class.

By the end of this session, you will be able to compare dates in Java with confidence and select the approach that best meets your needs.

Step By Step Guide On Date Comparison In Java :-

Method 1 - compareTo()

Both the java.util.Date class and the java.time.LocalDate class contain this method.

The relative order of the two dates is represented by an integer number that is returned.

import java.time.LocalDate;
import java.util.Date;
public class DateComparison {
    public static void main(String[] args) {
        // Method 1: compareTo() using java.util.Date
        Date date1 = new Date();
        Date date2 = new Date(123,12,18);
        int comparisonResult1 = date1.compareTo(date2);
        if (comparisonResult1==1){
            System.out.println("Given Date is Before then Current Date");
        }else if (comparisonResult1==-1){
            System.out.println("Given date is After then Current Date");
        }
        else {
            System.out.println("Given date and today date are same");
        }
    }
}
  1. Date1 and Date2 are produced as two Date objects. While date2 denotes a specific date, date1 represents the current date.
  2. Date1 is supplied to the compareTo() method along with Date2 as an input. It provides an integer value back.
  3. If the answer is 1, then date1 is earlier than date2.
  4. If the outcome is -1, date1 is prior to date2, and vice versa.
  5. If the outcome is 0, it signifies that dates 1 and 2 are identical.
  6. The necessary messages are printed based on the comparison result.

Method 2

The equals() method examines two dates to see if they are

equal. If the dates are equal, it returns true; otherwise, it returns false.

import java.time.LocalDate;
import java.util.Date;
public class DateComparison {
    public static void main(String[] args) {
        // Method 2: equals() using java.util.Date
        boolean isEqual = date1.equals(date2);
        if (isEqual==true){
            System.out.println("Given Dates are same");
        }
        else{
            System.out.println("Given Dates are not equal");
        }
    }
}
  1. Date1 is supplied to the equals() method along with Date2 as an input. A boolean result showing whether the two dates are equal is what is returned.
  2. The words "Given Dates are Same" are printed if the dates are same. If not, the warning "Given Dates are not equal" is displayed on the screen.

Method 3

The isBefore() and isAfter() methods can be found in the java.time package.Classes LocalDate.

They determine whether a date is before to or subsequent to another date. Depending on the outcome of the comparison, they either return true or false.

import java.time.LocalDate;
import java.util.Date;
public class DateComparison {
    // Method 3: isBefore() and isAfter() using java.time.LocalDate
        LocalDate localDate1 = LocalDate.now();
        LocalDate localDate2 = LocalDate.now().plusDays(1);
        boolean isBefore = localDate1.isBefore(localDate2);
        boolean isAfter = localDate1.isAfter(localDate2);
        if (isBefore==true){
            System.out.println("Given Date1 is before then date 2");
        }else {
            System.out.println("Given Date1 is after then date 2");
        }
    }
}
  1. It creates two LocalDate objects, localDate1 and localDate2. LocalDate1 denotes the day of the week, while LocalDate2 denotes the day after.
  2. On localDate1, the isBefore() and isAfter() functions are used to compare it to localDate2.
  3. The message "Given Date1 is before then date 2" is printed if localDate1 is earlier than localDate2. The message "Given Date1 is after then date 2" is printed in the absence of that.

Conclusion :-

In this article, we looked at various Java date comparison techniques. We reviewed the Java.util.Date class's methods as well as those in the more recent java.time package.

These techniques make it simple to compare dates and determine if they are equal or in a particular sequence.

It's crucial to select the right approach based on the particular needs of your application.

You are ready to perform date comparison jobs in Java successfully after completing this tutorial.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪