How to Compare Dates in Java

How to Compare Dates in Java

In this tutorial, you will figure out how to compare dates in Java.

There are essentially three distinct approaches to compare two dates in Java.

  1. Utilizing compareTo() method
  2. Utilizing equals(), previously() and after() method
  3. Utilizing getTime() method

Beneath I have shared one example for every one of these methods.

How to Compare Dates in Java

How to Compare Dates in Java

compareTo()

We can utilize compareTo() method of java.util.Date class to compare dates in Java. Let say we are contrasting date1 and date2 by date1.compareTo(date2). It returns following outcome.

Return 0 if date1 and date2 are equivalent.

Return esteem under 0 if date1 is before date2.

Return esteem more than 0 if date1 is after date2.

package com;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class CompareDates{
	public static void main(String args[]){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			Date d1=df.parse("2016-01-3");
			Date d2=df.parse("2016-01-3");
			
			if(d1.compareTo(d2)==0){
				System.out.println("Date1 and Date2 are equal");
			}
			else if(d1.compareTo(d2)<0){
				System.out.println("Date1 is before Date2");				
			}
			else if(d1.compareTo(d2)>0){
				System.out.println("Date1 is after Date2");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}		
	}
}

Output:

Date1 and Date2 are equivalent 

equals(), previously() and after()

These are easy to use methods used to compare dates in Java. They are accessible in java.util.Date of class.

equals() method returns genuine if two dates are equivalent, otherwise returns bogus.

previously() method returns genuine if the first date is before the second date, otherwise returns false.

after() method returns genuine if the first date is after the second date, otherwise returns false.

package com;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class CompareDates{
	public static void main(String args[]){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			Date d1=df.parse("2016-01-5");
			Date d2=df.parse("2016-01-7");
			
			if(d1.equals(d2)){
				System.out.println("Date1 and Date2 are equal");
			}
			else if(d1.before(d2)){
				System.out.println("Date1 is before Date2");				
			}
			else if(d1.after(d2)){
				System.out.println("Date1 is after Date2");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}		
	}
}

Output:

Date1 is before Date2

getTime()

The getTime() method of java.util.Date class can be additionally used to compare dates in Java.

getTime() method restores the number of milliseconds since January 1, 1970. Beneath example shows how it tends to be utilized.

package com;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class CompareDates{
	public static void main(String args[]){
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			Date d1=df.parse("2016-01-5");
			Date d2=df.parse("2016-01-3");
			
			if(d1.getTime()==d2.getTime()){
				System.out.println("Date1 and Date2 are equal");
			}
			else if(d1.getTime()<d2.getTime()){
				System.out.println("Date1 is before Date2");				
			}
			else if(d1.getTime()>d2.getTime()){
				System.out.println("Date1 is after Date2");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}		
	}
}

Output:

Date1 is after Date2

On the off chance that you discovered anything inaccurate or have questions identified with above

Java compares dates tutorial then don’t hesitate to remark underneath.

Leave a Comment

error: Alert: Content is protected!!