How to compare dates in Java
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(); } } }

Hi!
How to get actualSystemDate?
Regards.
Thank you kindly.
When the dates are equal both the System.out.println(“Date1 is after Date2″); and System.out.println(“Date1 is equal Date2″); will trigger, how to fix this please.
It is not working properly.
Below please check below code.
is not working properly.
Thanks guys, it helped me a lot in my project development.
Really helpful… Keep writing..:-)
I would like to point out that if you compare with equals two date with different format:
the method will return false (of course). That is why more often you can see that Java developer uses Calendar instead.
Christian, you made a good point here. Thanks for sharing.
I just tried the code for android,but don’t get any output.What I am doing is the same.just had to put the date.parse() part in try catch loop apart from that no change and I checked for the equality on click of a button.. but nothing is happening with the code.I put log.i() statements for debugging but again in vain. I guess its not parsing the date.can you help me with it.
I am posting my code so if you can help me I would be grateful to you
c = Calendar.getInstance();
creationyear= c.get(Calendar.YEAR);
creationmonth= c.get(Calendar.MONTH);
creationday= c.get(Calendar.DAY_OF_MONTH);
cdate=Integer.toString(creationyear)+”-”+Integer.toString(creationmonth+5)+”-”+Integer.toString(creationday);
ndate=Integer.toString(creationyear+1)+”-”+Integer.toString(creationmonth+6)+”-”+Integer.toString(creationday+3);
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd”);
try
{
date1=sdf.parse(cdate);
Log.i(“date1″,”date1″);
}
catch (ParseException e)
{
e.printStackTrace();
}
and next piece of code is written on button click
if(date1.compareTo(date2)>0){
Log.i(“Result”, “Date1 is after Date2″);
}
else if(date1.compareTo(date2)<0)
{
Log.i("Result","Date1 is before Date2");
}
else if(date1.compareTo(date2)==0)
{
Log.i("Result","Date1 is equal to Date2");
}
else
{
Log.i("Result","How to get here?");
}
i like this code …….great job
Great job…
Just what I wanted )
Thanks!
good example but little bit complected this example also consider time when comparing two date objects. Need to use dateformat , so it will help to remove time part.
Nice. So helpful.