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.   
  5. public class DateComparison {  
  6.     private static void DateComparison() {  
  7.   
  8.         Date comparetodaydate  = new Date();  
  9.         Date comparedate  = (Date) comparetodaydate.clone();  
  10.         Date comparedate1970  = new Date(24L*60L*60L*1000L);  
  11.   
  12.         System.out.println("Comparison of two dates by using the equals() method:");  
  13.         System.out.println();  
  14.         System.out.println("Today's date:" + comparetodaydate);  
  15.         System.out.println("Compairing date:" + comparedate);  
  16.   
  17.         if (comparetodaydate.equals(comparedate)) {  
  18.             System.out.println("The two dates are equal");  
  19.         }   
  20.         else {  
  21.             System.out.println("The two dates are not equal");  
  22.         }  
  23.   
  24.         System.out.println();  
  25.         System.out.println("Comparison of two dates by using the equals() method:");  
  26.         System.out.println();  
  27.         System.out.println("Today's date:" + comparetodaydate);  
  28.         System.out.println("Compairing date:" + comparedate1970);  
  29.   
  30.         if (comparetodaydate.equals(comparedate1970)) {  
  31.             System.out.println("The two dates are equal");  
  32.         }   
  33.         else {  
  34.             System.out.println("The two dates are not equal");  
  35.         }  
  36.           
  37.         System.out.println();  
  38.         System.out.println("Comparison of two dates by using the before() method:");  
  39.         System.out.println();  
  40.         System.out.println("Today's date:" + comparetodaydate);  
  41.         System.out.println("Compairing date:" + comparedate1970);  
  42.   
  43.         if (comparetodaydate.before(comparedate1970)) {  
  44.             System.out.println("Today's date comes before the compairing date");  
  45.         }   
  46.         else {  
  47.             System.out.println("Today's date does not come before the compairing date");  
  48.         }  
  49.           
  50.         System.out.println();  
  51.         System.out.println("Comparison of two dates by using the after() method::");  
  52.         System.out.println();  
  53.         System.out.println("Today's date:" + comparetodaydate);  
  54.         System.out.println("Compairing date:" + comparedate1970);  
  55.           
  56.         if (comparetodaydate.after(comparedate1970)) {  
  57.             System.out.println("Today's date comes after the compairing date");  
  58.         }   
  59.         else {  
  60.             System.out.println("Today's date does not come after the compairing date");  
  61.             System.out.println();  
  62.         }  
  63.         System.out.println();     
  64.     }  
  65.   
  66.     public static void main(String[] args) {  
  67.         System.out.println();  
  68.         DateComparison();  
  69.     }  

Output

Ebook Download
View all
Learn
View all