Date and Time

Java compare dates

int compareTo(T o)

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)

package com.candidjava.datetime;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;

public class CompareDates {
	public static void main(String[] args) {
		
		try {
			// Using Compareto() Method of Date Class
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
			Date date1 = dateFormat.parse("28-08-1996");
			Date date2 = dateFormat.parse("28-08-1996");
			System.out.println("Using Compareto() Method:");
			if (date1.compareTo(date2) > 0) {
				System.out.println(dateFormat.format(date1) + " comes after " + dateFormat.format(date2));
			} else if (date1.compareTo(date2) < 0) {
				System.out.println(dateFormat.format(date1) + " comes before " + dateFormat.format(date2));
			} else if (date1.compareTo(date2) == 0) {
				System.out.println("Both dates are equal");
			}
			System.out.println("---------------------------------------------------");
			// Using Before(),After(),Equals() Method of Date Class
			System.out.println("Using Before(),After(),Equals() method:");
			if (date1.after(date2)) {
				System.out.println(dateFormat.format(date1) + " comes after " + dateFormat.format(date2));
			} else if (date1.before(date2)) {
				System.out.println(dateFormat.format(date1) + " comes before " + dateFormat.format(date2));
			} else if (date1.equals(date2)) {
				System.out.println("Both dates are equal");
			}
			System.out.println("---------------------------------------------------");
			// Using Calendar Class
			Calendar calendar1 = Calendar.getInstance();
			Calendar calendar2 = Calendar.getInstance();
			calendar1.setTime(date1);
			calendar2.setTime(date2);
			System.out.println("Using Calendar class:");
			if (calendar1.after(calendar2)) {
				System.out.println(dateFormat.format(date1) + " comes after " + dateFormat.format(date2));
			} else if (calendar1.before(calendar2)) {
				System.out.println(dateFormat.format(date1) + " comes before " + dateFormat.format(date2));
			} else if (calendar1.equals(calendar2)) {
				System.out.println("Both dates are equal");
			}
			System.out.println("---------------------------------------------------");
			// Using LocalDate Class
			LocalDate localdate1 = LocalDate.of(1996, 8, 28);
			LocalDate localdate2 = LocalDate.of(1996, 7, 28);
			System.out.println("Using LocalDate Class:");
			if (localdate1.isAfter(localdate2)) {
				System.out.println(localdate1 + " comes after " + localdate2);
			} else if (localdate1.isBefore(localdate2)) {
				System.out.println(localdate1 + " comes before " + localdate2);
			} else if (localdate1.isEqual(localdate2)) {
				System.out.println("Both dates are equal");
			}
			System.out.println("---------------------------------------------------");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

}

OUTPUT

Using Compareto() Method:
Both dates are equal
---------------------------------------------------
Using Before(),After(),Equals() method:
Both dates are equal
---------------------------------------------------
Using Calendar class:
Both dates are equal
---------------------------------------------------
Using LocalDate Class:
1996-08-28 comes after 1996-07-28
---------------------------------------------------