how to compare two string dates using the compareto method 3f

Solutions on MaxInterview for how to compare two string dates using the compareto method 3f by the best coders in the world

showing results for - "how to compare two string dates using the compareto method 3f"
Michele
14 Jan 2020
1package javaapplication;
2
3import java.text.ParseException;
4import java.text.SimpleDateFormat;
5import java.util.Date;
6
7public class JavaApplication {
8
9    public static void main(String args[]) {
10
11        String fromDateString = "2021-06-15";
12        
13        String toDateString = "2021-07-15";
14
15        try {
16            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
17
18            Date dateFrom = dateFormat.parse(fromDateString);
19            
20            Date dateTo = dateFormat.parse(toDateString);
21            
22         
23            if (dateFrom.compareTo(dateTo) > 0) {
24                System.out.println("From Date is greater than To Date");
25                
26            } else if (dateFrom.compareTo(dateTo) < 0) {
27                
28               System.out.println("From Date is less than To Date");
29               
30            } else if (dateFrom.compareTo(dateTo) == 0) {
31                
32                  System.out.println("From Date is Equal to To Date");
33            }
34
35        } catch (ParseException e) {
36            e.printStackTrace();
37        }
38
39    }
40
41}
42 
similar questions