How to compare two dates in java / Various methods of date comparison in java

Comparison between two dates is a task that every developer has to perform. Comparison implies determining which date is earlier or later of the two dates. It would be great if we are aware of different ways in which we can achieve it. Below are some of the ways for comparing two dates using java api classes and their methods.

Method 1 : Using after and before methods of java.util.Date class

public void getLatestDate(){
    String startDateStr = "27/04/2016";
    String endDateStr = "28/04/2016";
    //create a date formatter
    SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/mm/yyyy");
    //parse the date string to convert it into java.util.date object
    Date startDate = dateFormatter.parse(startDateStr);
    Date endDate = dateFormatter.parse(endDateStr);
    if(startDate.after(endDate)){
      return startDate;
    } else {
      return endDate;
    }
}

Detail : Convert a date in string format to a java.util.Date object by parsing it using a java.text.SimpleDateFormat object. Then use after() method of java.util.Date class to compare two dates. after() method takes a date object as argument and compares it with the date object which calls this method.
Returns true if the date object which calls after represents a later date than the date object which is passed as argument and false if the date calling this method has an earlier date than the date object passed as argument.
Note : You can also use before() method of java.util.Date class which does the opposite of after() method.

Method 2 : Using compareTo method of java.util.Date class

public void getLatestDate(){
    String startDateStr = "27/04/2016";
    String endDateStr = "28/04/2016";
    //create a date formatter
    SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/mm/yyyy");
    //parse the date string to convert it into java.util.date object
    Date startDate = dateFormatter.parse(startDateStr);
    Date endDate = dateFormatter.parse(endDateStr);
    int result = startDate.compareTo(endDate)
    if(result<0){
      return endDate;
    } else {
      return startDate;
    }
}

Detail : Convert a date in string format to a java.util.Date object by parsing it using a java.text.SimpleDateFormat object. Then use compareTo() method of java.util.Date class to compare two dates. compareTo() method takes a date object as argument and compares it with the date object which calls this method and returns an integer which may have 3 possible values:

  •   0 -> the date which calls the method is same as the date passed as argument.
  •  -1 -> the date which calls the method is earlier than the date passed as argument.
  •   1 -> the date which calls the method is later than the date passed as argument.

Method 3 : Using after and before methods of java.util.Calendar class

public void getLatestDate(){
String startDateStr = "27/04/2016";
String endDateStr = "28/04/2016";
//create a date formatter
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/mm/yyyy");
//parse the date string to convert it into java.util.date object
Date startDate = dateFormatter.parse(startDateStr);
Date endDate = dateFormatter.parse(endDateStr);
//get a calendar set to default time
Calendar start = Calendar.getInstance();
//set the calendar to the required start date
start.setTime(startDate);
Calendar end = Calendar.getInstance();
//set the calendar to the required end date
end.setTime(endDate);
if(start.after(end)){
return startDate;
}else{
return endDate;
}
}

Detail : Convert a date in string format to a java.util.Date object by parsing it using a java.text.SimpleDateFormat object. Then use after() method of java.util.Calendar class to compare two dates. after() method takes a calendar object as argument and compares it with the calendar object which calls this method.
Returns true if the calendar object which calls after represents a later date than the calendar object which is passed as argument and false if the calendar object calling this method has an earlier date than the calendar object passed as argument.
Note: You can also use before() method of java.util.Calendar class which does the opposite of after() method.

Method 4 : Using compareTo method of java.util.Calendar class

public void getLatestDate(){
String startDateStr = "27/04/2016";
String endDateStr = "28/04/2016";
//create a date formatter
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/mm/yyyy");
//parse the date string to convert it into java.util.date object
Date startDate = dateFormatter.parse(startDateStr);
Date endDate = dateFormatter.parse(endDateStr);
//get a calendar set to default time
Calendar start = Calendar.getInstance();
//set the calendar to the required start date
start.setTime(startDate);
Calendar end = Calendar.getInstance();
//set the calendar to the required end date
end.setTime(endDate);
int result = start.compareTo(end)
if(result<0){
return endDate;
}else{
return startDate;
}
}

Detail : Convert a date in string format to a java.util.Date object by parsing it using a java.text.SimpleDateFormat object. Then use compareTo() method of java.util.Calendar class to compare two dates. compareTo() method takes a calendar object as argument and compares it with the calendar object which calls this method and returns an integer which may have 3 possible values:

  •   0 -> the date represented by the calendar which calls the method is same as the date represented by the calendar passed as argument.
  •  -1 -> the date represented by the calendar which calls the method is earlier than the date represented by the calendar passed as argument.
  •   1 -> the date represented by the calendar which calls the method is later than the date represented by the calendar passed as argument.

Let’s tweak in :

    1. parse() method of java.text.SimpleDateFormat throws ParseException if the format of date it expects does not match with that of the date given to it. For example, if we initialize java.text.SimpleDateFormatas new SimpleDateFormat("dd-mm-yyyy") and use it to parse a date of format a date as “27/06/2016” then we will get java.text.ParseException: Unparseable date: "27/04/2016" since it expects the date as 27-04-2016 (Notice the “/” and “-” as date component separators)
    2. java.text.ParseException thrown by parse() method is a checked exception and hence it needs to be caught or declared to thrown by the method in which parse() is used.
    3. after() and before() methods of java.util.Calendar class take java.lang.Object as argument which means we can pass anything to it. For Example, we can write startDate.after("today"). But an argument of type other than java.util.Calendar will return false, though it will not give any error.
    4. Calendar.getInstance() is a static method of java.util.Calendar class and returns the current system time till a custom date and time is set using its setTime() method

Leave a Reply