Date Comparison

In this section we are discussing the comparison of two dates, whether they are equal or not, the first date comes before the other or not, the first date comes after the other or not, by using the equals(), before() and after() methods of the date class.

Date Comparison

In this section we are discussing the comparison of two dates, whether they are equal or not, the first date comes before the other or not, the first date comes after the other or not, by using the equals(), before() and after() methods of the date class.

Date Comparison

Date Comparison

     

In this section we are discussing the comparison of two dates, whether they are equal or not, the first date comes before the other or not, the first date comes after the other or not, by using the equals(), before() and after() methods of the date class.

Description of program:  

In this program first we are write the method DateComparison() in which we create an object comparetodaydate of the Date class that have current date and time and a clone of this object. We also create one more object comparedate1970 of the Date class that contains a date of 1970. Now we are comparing these two dates whether they are equal or not by using the equals() method, comparison of the date contained in the object comparetodaydate comes before the date contained in the object comparedate1970 or not, and date contained in the object comparetodaydate comes after the date contained in the object comparedate1970 or not and then printing all the values on the console. 


Here is the code  of this program:
    

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateComparison {
  private static void DateComparison() {

    Date comparetodaydate  = new Date();
    Date comparedate  = (Date) comparetodaydate.clone();
    Date comparedate1970  = new Date(24L*60L*60L*1000L);

    System.out.println("Comparison of two dates by using the equals() method:");
    System.out.println();
    System.out.println("Today's date: " + comparetodaydate);
    System.out.println("Comparing date: " + comparedate);

    if (comparetodaydate.equals(comparedate)) {
            System.out.println("The two dates are equal");
    } 
    else {
            System.out.println("The two dates are not equal");
    }

    System.out.println();
    System.out.println("Comparison of two dates by using the equals() method:");
    System.out.println();
    System.out.println("Today's date: " + comparetodaydate);
    System.out.println("Compairing date: " + comparedate1970);

    if (comparetodaydate.equals(comparedate1970)) {
      System.out.println("The two dates are equal");
    } 
    else {
            System.out.println("The two dates are not equal");
    }
    
    System.out.println();
    System.out.println("Comparison of two dates by using the before() method:");
    System.out.println();
    System.out.println("Today's date: " + comparetodaydate);
    System.out.println("Compairing date: " + comparedate1970);

    if (comparetodaydate.before(comparedate1970)) {
            System.out.println("Today's date comes before the compairing date");
    } 
    else {
            System.out.println("Today's date does not come before the comparing date");
    }
        
    System.out.println();
    System.out.println("Comparison of two dates by using the after() method::");
    System.out.println();
    System.out.println("Today's date: " + comparetodaydate);
    System.out.println("Comparing date: " + comparedate1970);
        
    if (comparetodaydate.after(comparedate1970)) {
            System.out.println("Today's date comes after the comparing date");
    } 
    else {
            System.out.println("Today's date does not come after the comparing date");
            System.out.println();
    }
    System.out.println();   
  }

    public static void main(String[] args) {
        System.out.println();
        DateComparison();
    }
}


Here is the output of above program:

C:\Examples>java DateComparison

Comparison of two dates by using the equals() method:

Today's date: Mon Dec 10 18:24:59 GMT+05:30 2007
Comparing date: Mon Dec 10 18:24:59 GMT+05:30 2007
The two dates are equal

Comparison of two dates by using the equals() method:

Today's date: Mon Dec 10 18:24:59 GMT+05:30 2007
Comparing date: Fri Jan 02 05:30:00 GMT+05:30 1970
The two dates are not equal

Comparison of two dates by using the before() method:

Today's date: Mon Dec 10 18:24:59 GMT+05:30 2007
Comparing date: Fri Jan 02 05:30:00 GMT+05:30 1970
Today's date does not come before the comparing date

Comparison of two dates by using the after() method::

Today's date: Mon Dec 10 18:24:59 GMT+05:30 2007
Comparing date: Fri Jan 02 05:30:00 GMT+05:30 1970
Today's date comes after the comparing date


Download of this program.