I need to compare the two dates and know whether they are equal or < or >
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();
}
}
}
the above Code doesnt work properly.. I tried this format MM-dd-YYYYit is checking oly if the year is > than the first date it gives >. Eg: i compared in this way d1 = 01-07-2014 d2 = 01-06-2014.. result should be d1 >d2 but it tells d1 is equal to d2
Kindly help me