java Compare two dates

date, java

Solution

java.util.Date class has before and after method to compare dates.

Date date1 = new Date();
Date date2 = new Date();

if(date1.before(date2)){
    //Do Something
}

if(date1.after(date2)){
    //Do Something else
}

Problem

I want to compare two dates and check if the date has expired or not. Here is the code I used : ``` SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:ss:ii"); Date date1 = sdf.parse("20012-10-4 10:15:25"); Date date2 = sdf.parse("2013-10-4 10:15:25"); if(date1.equals(date12)){ System.out.println("Both are equals"); } ``` I want to check the two dates but without success. I also tried to check it like that : ``` if(date1 >= date2){ System.out.println("Both are not equals"); } ``` But it's not working either.

Original source

Related problems