Java compare dates

Java compare dates using Date.compareTo()

package com.w3spoint;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class CompareDateTest {
  public static void main(String args[]){
	  try {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        Date date1 = sdf.parse("2018-04-09");
	        Date date2 = sdf.parse("2017-01-31");
 
	        System.out.println("date1 : " + sdf.format(date1));
	        System.out.println("date2 : " + sdf.format(date2));
 
	        if (date1.compareTo(date2) > 0) {
	            System.out.println("Date1 is after Date2");
	        } else if (date1.compareTo(date2) < 0) {
	            System.out.println("Date1 is before Date2");
	        } else if (date1.compareTo(date2) == 0) {
	            System.out.println("Date1 is equal to Date2");
	        }
	} catch (Exception e) {
		e.printStackTrace();
	}
  }
}

Output:

date1 : 2018-04-09
date2 : 2017-01-31
Date1 is after Date2

Java compare dates using Date.before(), Date.after() and Date.equals()

package com.w3spoint;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class CompareDateTest {
  public static void main(String args[]){
	  try {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        Date date1 = sdf.parse("2018-04-09");
	        Date date2 = sdf.parse("2017-01-31");
 
	        System.out.println("date1 : " + sdf.format(date1));
	        System.out.println("date2 : " + sdf.format(date2));
 
	        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 Date2");
	        }
	} catch (Exception e) {
		e.printStackTrace();
	}
  }
}

Output:

date1 : 2018-04-09
date2 : 2017-01-31
Date1 is after Date2

Java compare dates using Calender.before(), Calender.after() and Calender.equals()

package com.w3spoint;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class CompareDateTest {
  public static void main(String args[]){
	  try {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        Date date1 = sdf.parse("2018-04-09");
	        Date date2 = sdf.parse("2017-01-31");
 
	        System.out.println("date1 : " + sdf.format(date1));
	        System.out.println("date2 : " + sdf.format(date2));
 
	        Calendar cal1 = Calendar.getInstance();
	        Calendar cal2 = Calendar.getInstance();
	        cal1.setTime(date1);
	        cal2.setTime(date2);
 
	        if (cal1.after(cal2)) {
	            System.out.println("Date1 is after Date2");
	        }
 
	        if (cal1.before(cal2)) {
	            System.out.println("Date1 is before Date2");
	        }
 
	        if (cal1.equals(cal2)) {
	            System.out.println("Date1 is equal Date2");
	        }
	} catch (Exception e) {
		e.printStackTrace();
	}
  }
}

Output:

date1 : 2018-04-09
date2 : 2017-01-31
Date1 is after Date2
Content Protection by DMCA.com
Please Share