Date comparison using Date and Calendars’s before(), after() and equals() method | Also using Date.compareTo() method in java


Contents of page :
  • Program 1) Using Date.before(), Date.after() and Date.equals() methods for date comparisons
  • Program 2) Using Calendar's before(), after() and equals() methods for date comparisons
  • Program 3) Using Date.compareTo() methods for date comparisons



Program 1) Using Date.before(), Date.after() and Date.equals() methods for date comparisons

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
//Using Date.before(), Date.after() and Date.equals()
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) throws ParseException {
          SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
          Date date1 = sdf.parse("21-03-2016");
          Date date2 = sdf.parse("29-08-2016");
          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 to date2");
          } else
                 System.out.println("date1 is NOT equal to date2");
          System.out.println("\n---Now, let's test equals method by making date1 and date2 equal--");
          date1 = sdf.parse("21-03-2016");
          date2 = sdf.parse("21-03-2016");
          if (date1.equals(date2)) {
                 System.out.println("date1 is equal to date2");
          } else {
                 System.out.println("date1 is NOT equal to date2");
          }
   }
}
/*OUTPUT
date1 is before date2
date1 is NOT equal to date2
---Now, let's test equals method by making date1 and date2 equal--
date1 is equal to date2
*/





Program 2) Using Calendar's before(), after() and equals() methods for date comparisons
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
//Using Calendar's before(), after() and equals() methods
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) throws ParseException {
          SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
          Date date1 = sdf.parse("21-03-2016");
          Date date2 = sdf.parse("29-08-2016");
         
          Calendar calendar1=new GregorianCalendar();
          Calendar calendar2=new GregorianCalendar();
          calendar1.setTime(date1);
          calendar2.setTime(date2);
          if (calendar1.after(calendar2)) {
                 System.out.println("calendar1 is after calendar2");
          }
          if (calendar1.before(calendar2)) {
                 System.out.println("calendar1 is before calendar2");
          }
          if (date1.equals(date2)) {
                 System.out.println("calendar1 is equal to calendar2");
          } else
                 System.out.println("calendar1 is NOT equal to calendar2");
   }
}
/*OUTPUT
calendar1 is before calendar2
calendar1 is NOT equal to calendar2
*/





Program 3) Using Date.compareTo() methods for date comparisons
package date_f3_compare__date_compare;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//Using Date.compareTo() method
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) throws ParseException {
          //if date1 is before date2 - than method returns -1
          //if date1 is equal to date2 - than method returns 0
          //if date1 is after date2 - than method returns 1
         
          SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
          Date date1 = sdf.parse("21-03-2016");
          Date date2 = sdf.parse("29-08-2016");
          // date1 is before date2 - so, method will return -1 and we won't enter below if.
          if (date1.compareTo(date2) > 0) {
                 System.out.println("date1 is after date2");
          }
          // date1 is before date2 - so, method will return -1 and enter below if.
          if (date1.compareTo(date2) < 0) {
                 System.out.println("date1 is before date2");
          }
          // date1 is before date2 - so, method will return -1
          //and we won't enter below if, we'll enter else.
          if (date1.compareTo(date2) == 0) {
                 System.out.println("date1 is equal to date2");
          } else {
                 System.out.println("date1 is NOT equal to date2");
          }
   }
}
/*OUTPUT
date1 is before date2
date1 is NOT equal to date2
*/



RELATED LINKS>

Convert Calendar and GregorianCalendar to Date | Convert Date to Calendar and GregorianCalendar in java




Convert Date to String>

Using SimpleDateFormat to convert Date to String - use dd-MM-yyyy and dd-MM-yyyy hh:mm:ss format




eEdit
Must read for you :