How to compare dates in Java
Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if date1 is after date2, and returns a price "an awful lot less than zero" if date1 is earlier than date2.
The Date elegance represents a particular on-the-spot in time, with millisecond precision. It provides constructors and strategies to address date and time with Java. To make this challenge easy Java affords compareTo(), before(), after(), and equals() method. In this segment, we will learn how to compare dates in Java, there are four instructions.
For compare dates in java, there are four classes. These are:
- Using compareTo() Method
- Using LocalDate Class Method
- Using Date Class Method
- Using Calendar Class Method
Using compareTo() Method: We can use Date.compareTo() method by using some simple steps-
- At first, create an object of simple date format.
- Initializing the date format like yyyy-mm-dd.
- After that, initialize some variables in the date mentioned above format (yyyy-mm-dd) Then examine dates using the compareTo() approach.
- When the process mentioned above is completed, then we print the result.
Using after(), before() and equals() Method: We can use Date.after(), Date.before() and Date.equals() method by using some simple step-
- At first, create an object of simple date format.
- Initializing the date format like yyyy-mm-dd.
- After that, initialize some variables in the date as mentioned above format (yyyy-mm-dd) Then compare two dates using date.after(), Date.before(), Date.equals() method.
- When the process mentioned above is completed, then we print the result.
Using calender.after(), calender.before() and calender.equals() Method: We can use Calendar.after(), Calender.before() and Calender.equals() method by using some simple step-
- At first, create an object of simple date format.
- Initializing the date format like yyyy-mm-dd.
- After that, initialize some variables in the date as mentioned above format (yyyy-mm-dd). Initialize the Calendar class objects the usage of the get instance() functions.
- The time() function of Calendar magnificence assigns the values to the calendar items.
- Use after() and before features of the Calendar elegance for the comparisons of dates When the process mentioned above is completed, we print the result.
Syntax of Date.compareTo() method in Java: Every function have some syntax. So, In Java, the Date.compareTo() method has a syntax.
public int compare To (Date another Date)
Returns value: The Date.compareTo() method can return some integer values like 0, less than 0 and greater than 0.
When both the dates are equal, this function returns the value 0. When the date is before the date of argument, then this function returns a value which is less than 0.
When the date is after the date of argument, this function returns a value greater than 0.
Note: Whilst copying the date in Java, take into account to import Java. text.SimpleDateFormat, java.text.ParseException, java.util.Date. allow implement the compareTo() technique and compares two dates.
Within the following example, we've created an example of the SimpleDateFormat elegance that allows us to take different date codecs. After that, we have taken variables, date1, and date2, of kind Date. the use of the parse() technique of the SimpleDateFormat class, we've got parsed the dates to examine. We've got handed the date1 and date2 variables of type date inside the layout() manner.
The method offers the formatted date string or time string. We've used the compareTo() technique to compare the two dates. If both dates are identical, it prints both dates are equal.
Then it returns the value 0. If the date1 is extra than the date2 then it prints Date 1 comes after Date 2. It means, When the date is before the date of argument, this function returns the value which is less than 0, and when the date is after the date of argument, then this function returns the value which is greater than 0.
Example 1: Right here, we supply an instance of the use of the Date.compareTo() technique in Java.
import java.text.*;
import java.util.Date;
public class CompareTwoDatesTest
{
public static void main (String[] args) throws ParseException
{
SimpleDateFormat sdformat = new SimpleDateFormat (“yyyy-mm-dd”);
// the dates initialize
Date d1 = sdformat . parse (“2022-09-08”);
Date d2 = sdformat . parse (“2022-09-08”);
//Print the dates
System.out.println (“The date 1 is : ” + sdformat . format (d1));
System.out.println (“The date 2 is : ” + sdformat . format (d2));
//Compare the dates
if (d1 . compareTo(d2) < 0)
{
System.out.println (“Date 1 is occurs before Date 2”);
}
else if (d1 . compareTo(d2) > 0)
{
System.out.println (“Date 1 is occurs after Date 2”);
}
else if (d1 . compareTo(d2) == 0)
{
System.out.println (“Date 1 and Date 2 both dates are equal”);
}
}
}
Output: We will compile the program and also run it. After the execution, the result is equal to 0. Because date 1 is equal to date 2.
The date 1 is : 2022-09-08
The date 1 is : 2022-09-08
Date 1 and Date 2 both dates are equal
Example 2: Right here, we supply an instance of the use of the Date.compareTo() technique in Java.
\import java.text.*;
import java.util.Date;
public class CompareTwoDatesTest
{
public static void main (String[] args) throws ParseException
{
SimpleDateFormat sdformat = new SimpleDateFormat (“yyyy-mm-dd”);
//the dates initialize
Date d1 = sdformat . parse (“2022-09-08”);
Date d2 = sdformat . parse (“2022-09-20”);
//Print the dates
System.out.println (“The date 1 is : ” + sdformat . format (d1));
System.out.println (“The date 2 is : ” + sdformat . format (d2));
//Compare the dates
if (d1 . compareTo(d2) < 0)
{
System.out.println (“Date 1 is occurs before Date 2”);
}
else if (d1 . compareTo(d2) > 0)
{
System.out.println (“Date 1 is occurs after Date 2”);
}
else if (d1 . compareTo(d2) == 0)
{
System.out.println (“Date 1 and Date 2 both dates are equal”);
}
}
}
Output: We will compile the program and also run it. After the execution, the result is less than 0. Because date 1 occurs before date 2.
The date 1 is: 2022-09-08
The date 1 is: 2022-09-20
Date 1 is occurs before Date 2
Example 3: Right here, we supply an instance of the use of the Date.compareTo() technique in Java.
import java.text.*;
import java.util.Date;
public class CompareTwoDatesTest
{
public static void main (String[] args) throws ParseException
{
SimpleDateFormat sdformat = new SimpleDateFormat (“yyyy-mm-dd”);
//the dates initialize
Date d1 = sdformat . parse (“2022-09-10”);
Date d2 = sdformat . parse (“2022-09-02”);
//Print the dates
System.out.println (“The date 1 is : ” + sdformat . format (d1));
System.out.println (“The date 2 is : ” + sdformat . format (d2));
//Compare the dates
if (d1 . compareTo(d2) < 0)
{
System.out.println (“Date 1 is occurs before Date 2”);
}
else if (d1 . compareTo(d2) > 0)
{
System.out.println (“Date 1 is occurs after Date 2”);
}
else if (d1 . compareTo(d2) == 0)
{
System.out.println (“Date 1 and Date 2 both dates are equal”);
}
}
}
Output: We will compile the program and also run it. After the execution, the result is greater than 0. Because date 1 occurs after date 2.
The date 1 is: 2022-09-10
The date 1 is: 2022-09-02
Date 1 is occurs after Date 2
Example 4: Right here, we supply an instance of the use of the Date.compareTo() technique in Java. For the use of Java.util.Calendar, the Calendar works a similar way as Java.util.Date. And the Calendar incorporates the similar compareTo, before(), after() and equals() to evaluate calendar.
import java.test.SimpleDateFormat;
import java.util.Calender;
import java.util.ParseException;
import java.util.Date;
public class CompareCalender
{
public static void main (String[] args) throws ParseException
{
SimpleDateFormat sdformat = new SimpleDateFormat (“yyyy-mm-dd”);
//the dates initialize
Date d1 = sdformat . parse (“2022-09-10”);
Date d2 = sdformat . parse (“2022-08-20”);
Calendar cal1 = Calender.getInstance();
Calendar cal2 = Calender.getInstance();
Cal1.setTime(date1);
Cal2.setTime(date2);
//Print the dates
System.out.println (“The date 1 is : ” + sdformat . format (d1));
System.out.println (“The date 2 is : ” + sdformat . format (d2));
//Compare the dates
if (cal1 . before (cal2))
{
System.out.println (“Date 1 is occurs before Date 2”);
}
else if (cal1 . after (cal2))
{
System.out.println (“Date 1 is occurs after Date 2”);
}
else if (cal1 . equals (cal2))
{
System.out.println (“Date 1 and Date 2 both dates are equal”);
}
}
}
Output: We will compile the program and also run it. After the execution, the result is greater than 0. Because date 1 occurs after date 2.
The date 1 is: 2022-09-10
The date 1 is: 2022-08-20
Date 1 is occurs after Date 2
So, here we give some examples of the use of the compareTo() technique. We write the program and also share the output of the assigned programs.