Java Date Comparison

Introduction

  • In this blog, I am going to explain the program for the date comparison program in Java 

 Software Requirements

  • Java, Notepad.
Program
  1. import java.util.Date;  
  2. import java.text.ParseException;  
  3. import java.text.SimpleDateFormat;  
  4. public class DateComparison {  
  5.  private static void DateComparison() {  
  6.   Date comparetodaydate = new Date();  
  7.   Date comparedate = (Date) comparetodaydate.clone();  
  8.   Date comparedate1970 = new Date(24 L * 60 L * 60 L * 1000 L);  
  9.   System.out.println("Comparison of two dates by using the equals() method:");  
  10.   System.out.println();  
  11.   System.out.println("Today's date:" + comparetodaydate);  
  12.   System.out.println("Compairing date:" + comparedate);  
  13.   if (comparetodaydate.equals(comparedate)) {  
  14.    System.out.println("The two dates are equal");  
  15.   } else {  
  16.    System.out.println("The two dates are not equal");  
  17.   }  
  18.   System.out.println();  
  19.   System.out.println("Comparison of two dates by using the equals() method:");  
  20.   System.out.println();  
  21.   System.out.println("Today's date:" + comparetodaydate);  
  22.   System.out.println("Compairing date:" + comparedate1970);  
  23.   if (comparetodaydate.equals(comparedate1970)) {  
  24.    System.out.println("The two dates are equal");  
  25.   } else {  
  26.    System.out.println("The two dates are not equal");  
  27.   }  
  28.   System.out.println();  
  29.   System.out.println("Comparison of two dates by using the before() method:");  
  30.   System.out.println();  
  31.   System.out.println("Today's date:" + comparetodaydate);  
  32.   System.out.println("Compairing date:" + comparedate1970);  
  33.   if (comparetodaydate.before(comparedate1970)) {  
  34.    System.out.println("Today's date comes before the compairing date");  
  35.   } else {  
  36.    System.out.println("Today's date does not come before the compairing date");  
  37.   }  
  38.   System.out.println();  
  39.   System.out.println("Comparison of two dates by using the after() method::");  
  40.   System.out.println();  
  41.   System.out.println("Today's date:" + comparetodaydate);  
  42.   System.out.println("Compairing date:" + comparedate1970);  
  43.   if (comparetodaydate.after(comparedate1970)) {  
  44.    System.out.println("Today's date comes after the compairing date");  
  45.   } else {  
  46.    System.out.println("Today's date does not come after the compairing date");  
  47.    System.out.println();  
  48.   }  
  49.   System.out.println();  
  50.  }  
  51.  public static void main(String[] args) {  
  52.   System.out.println();  
  53.   DateComparison();  
  54.  }  
  55. }   
Output 
 
one