package nilpointer.net;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Demo {
public static void main(String[] args) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = simpleDateFormat.parse("20/10/2021");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Date date2 = simpleDateFormat.parse("23/10/2021");
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
System.out.println("Approach 1");
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");
}
System.out.println("Approach 2");
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 {
System.out.println("Date1 is equal to Date2");
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
Approach 1
Date1 is before Date2
Approach 2
Date1 is before Date2