I want to compare two dates and based on the solution provided in link How to compare dates in Java?, I used the below code.
SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy");
String strDate1 = "21-01-2021";
String strDate2 = "01-02-2021";
Date date1 = sdf.parse(strDate1);
Date date2 = sdf.parse(strDate2);
if (date1.after(date2)) {
System.out.println(strDate1 + " is after " + strDate2);
}
if (date1.before(date2)) {
System.out.println(strDate1 + " is before " + strDate2);
}
if (date1.equals(date2)) {
System.out.println("Both are same date");
}
The output I get is 21-01-2021 is after 01-02-2021
which is wrong. Jan 21 2021 is before Feb 01 2021.
Can someone please help me to resolve this. Thank you!!