Here are three examples to compare two dates in Java.

1. Date.compareTo()

A classic method to compare two dates in Java.

– Return value is 0 if both dates are equal.
– Return value is greater than 0 , if Date is after the date argument.
– Return value is less than 0, if Date is before the date argument.

package com.mkyong.date;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class App 
{
     public static void main( String[] args ) 
    {
    	try{
 
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        	Date date1 = sdf.parse("2009-12-31");
        	Date date2 = sdf.parse("2010-01-31");
 
        	System.out.println(sdf.format(date1));
        	System.out.println(sdf.format(date2));
 
        	if(date1.compareTo(date2)>0){
        		System.out.println("Date1 is after Date2");
        	}else if(date1.compareTo(date2)<0){
        		System.out.println("Date1 is before Date2");
        	}else if(date1.compareTo(date2)==0){
        		System.out.println("Date1 is equal to Date2");
        	}else{
        		System.out.println("How to get here?");
        	}
 
    	}catch(ParseException ex){
    		ex.printStackTrace();
    	}
    }
}

2. Date.before(), Date.after() and Date.equals()

A more user friendly method to compare two dates. Java developers seldom use this method, not really sure what may causing it, may be they just didn’t aware of it?

package com.mkyong.date;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class App 
{
    public static void main( String[] args ) 
    {
    	try{
 
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        	Date date1 = sdf.parse("2009-12-31");
        	Date date2 = sdf.parse("2010-01-31");
 
        	System.out.println(sdf.format(date1));
        	System.out.println(sdf.format(date2));
 
        	if(date1.after(date2)){
        		System.out.println("Date1 is after Date2");
        	}
 
        	if(date1.before(date2)){
        		System.out.println("Date1 is before Date2");
        	}
 
        	if(date1.equals(date2)){
        		System.out.println("Date1 is equal Date2");
        	}
 
    	}catch(ParseException ex){
    		ex.printStackTrace();
    	}
    }
}

3. Calender.before(), Calender.after() and Calender.equals()

Most common way to compare two dates with Calendar object, and believe it’s most widely used method.

package com.mkyong.date;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class App 
{
     public static void main( String[] args ) 
    {
    	try{
 
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        	Date date1 = sdf.parse("2009-12-31");
        	Date date2 = sdf.parse("2010-01-31");
 
        	System.out.println(sdf.format(date1));
        	System.out.println(sdf.format(date2));
 
        	Calendar cal1 = Calendar.getInstance();
        	Calendar cal2 = Calendar.getInstance();
        	cal1.setTime(date1);
        	cal2.setTime(date2);
 
        	if(cal1.after(cal2)){
        		System.out.println("Date1 is after Date2");
        	}
 
        	if(cal1.before(cal2)){
        		System.out.println("Date1 is before Date2");
        	}
 
        	if(cal1.equals(cal2)){
        		System.out.println("Date1 is equal Date2");
        	}
 
    	}catch(ParseException ex){
    		ex.printStackTrace();
    	}
    }
}
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.
My website mkyong.com is hosted by Liquid Web, I really like their fast and helpful support, it saved me a lot of time. Furthermore, their servers are extremely reliable, fast and stable, I don't remember when was the last time we had server downtime. Btw, they're offering $100 Coupon now, if you are sourcing for great hosting provider, go grab it.

Related Posts

Popular Posts