how to compare two string dates with different date formats in java

Solutions on MaxInterview for how to compare two string dates with different date formats in java by the best coders in the world

showing results for - "how to compare two string dates with different date formats in java"
Christopher
11 Jan 2017
1package javaapplication;
2
3import java.text.*;
4import java.util.Date;
5import java.text.ParseException;
6
7public class date {
8
9    public static void main(String[] args) {
10
11        String startDate = "21-08-2027";
12        String endDate = "21/08/2021";
13
14        try {
15
16            Date dateStart = new SimpleDateFormat("dd-MM-yyyy").parse(startDate);
17
18            Date dateEnd = new SimpleDateFormat("dd/MM/yyyy").parse(endDate);
19
20            if (dateStart.after(dateEnd)) {
21                System.out.println("Start Date is greater than End Date");
22
23            } else if (dateStart.before(dateEnd)) {
24
25                System.out.println("Start Date is less than End Date");
26
27            } else {
28
29                System.out.println("Start Date is Equal to End Date");
30            }
31
32        } catch (ParseException e) {
33            e.printStackTrace();
34        }
35
36    }
37}
38