How to compare two string dates with different date formats in Java
package javaapplication;
import java.text.*;
import java.util.Date;
import java.text.ParseException;
public class date {
public static void main(String[] args) {
String startDate = "21-08-2027";
String endDate = "21/08/2021";
try {
Date dateStart = new SimpleDateFormat("dd-MM-yyyy").parse(startDate);
Date dateEnd = new SimpleDateFormat("dd/MM/yyyy").parse(endDate);
if (dateStart.after(dateEnd)) {
System.out.println("Start Date is greater than End Date");
} else if (dateStart.before(dateEnd)) {
System.out.println("Start Date is less than End Date");
} else {
System.out.println("Start Date is Equal to End Date");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}