Feb 042020

ADM

How to compare dates in Java

  • 04 February 2020
  • ADM

 

 

 

How to compare dates in Java - images/logos/java.jpg

 

When it comes working with dates, the classes used are located in java.util package.

Here are some examples how to compare dates in Java using Date, Calendar and LocalDate (since Java 1.8) classes.

Date.compareTo()

Date class is located in java.util package. The compareTo method returns three possible values:

  • 0 if the argument Date is equal to this Date;
  • < 0 if this Date is before the Date argument;
  • > 0 if this Date is after the Date argument.
package com.admfactory.date;

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

public class DateCompare1 {
    public static void main(String[] args) {
	try {
	    System.out.println("Compare date example using Date.compareTo method.");
	    System.out.println();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    Date date1 = sdf.parse("2015-12-11");
	    Date date2 = sdf.parse("2017-01-15");

	    System.out.println("date1 : " + sdf.format(date1));
	    System.out.println("date2 : " + sdf.format(date2));
	    System.out.println();

	    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

Compare date example using Date.compareTo method.

date1 : 2015-12-11
date2 : 2017-01-15

Date1 is before Date2

Date.before(), Date.after() and Date.equals()

These three methods are much easy to be used.

package com.admfactory.date;

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

public class DateCompare2 {
    public static void main(String[] args) {
	try {
	    System.out.println("Compare date example using Date.after, Date.before, Date.equals method.");
	    System.out.println();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    Date date1 = sdf.parse("2015-12-11");
	    Date date2 = sdf.parse("2013-01-15");

	    System.out.println("date1 : " + sdf.format(date1));
	    System.out.println("date2 : " + sdf.format(date2));
	    System.out.println();

	    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 to Date2");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}

    }
}

Output

Compare date example using Date.after, Date.before, Date.equals method.

date1 : 2015-12-11
date2 : 2013-01-15

Date1 is after Date2

Calender.before(), Calender.after() and Calender.equals()

Another option is to use Calendar class.

package com.admfactory.date;

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

public class DateCompare3 {
    public static void main(String[] args) {
	try {
	    System.out.println(
		    "Compare date example using Calender.before(), Calender.after() and Calender.equals() method.");
	    System.out.println();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    Date date1 = sdf.parse("2015-12-11");
	    Date date2 = sdf.parse("2015-12-11");

	    System.out.println("date1 : " + sdf.format(date1));
	    System.out.println("date2 : " + sdf.format(date2));
	    System.out.println();

	    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 to Date2");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}

    }
}

Output

Compare date example using Calender.before(), Calender.after() and Calender.equals() method.

date1 : 2015-12-11
date2 : 2015-12-11

Date1 is equal to Date2

LocalDate.isBefore(), LocalDate.isAfter() and LocalDate.equals()

If you are using Java 8 you can use LocaDate class, located in java.time package.

package com.admfactory.date;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateCompare4 {
    public static void main(String[] args) {
	try {
	    System.out.println(
		    "Compare date example using LocalDate.isBefore(), LocalDate.isAfter() and LocalDate.equals() method.");
	    System.out.println();
	    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
	    LocalDate date1 = LocalDate.of(2015, 12, 11);
	    LocalDate date2 = LocalDate.of(2015, 12, 11);

	    System.out.println("date1 : " + dtf.format(date1));
	    System.out.println("date2 : " + dtf.format(date2));
	    System.out.println();

	    if (date1.isAfter(date2)) {
		System.out.println("Date1 is after Date2");
	    }

	    if (date1.isBefore(date2)) {
		System.out.println("Date1 is before Date2");
	    }

	    if (date1.equals(date2)) {
		System.out.println("Date1 is equal to Date2");
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}

    }
}

Note: Also LocaDate.compareTo can be used, similar with Date.compareTo.

Output

Compare date example using LocalDate.isBefore(), LocalDate.isAfter() and LocalDate.equals() method.

date1 : 2015-12-11
date2 : 2015-12-11

Date1 is equal to Date2

 

 

 

References