Java Compare two Date Values

Tags: Java Date

In this Java core tutorial we learn how to compare two java.util.Date objects in Java programming language using compareTo(), equals(), before() and after() methods.

Table of contents

  1. Compare two Date objects using compareTo() method
  2. Compare two Date objects for Equality
  3. Compare if a Date before another Date
  4. Compare if a Date after another Date

Compare two Date objects using compareTo() method

In Java, to compare two Date objects for ordering we can use the Date.compareTo() method which return int value of -1, 0 or 1 if the Date object is before, equal or after the Date object it compare to.

The following Java example code to show the compareTo() method return -1 when the Date object is before another Date object it compare to.

CompareDateExample1.java

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

public class CompareDateExample1 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/07/10 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/07/11 07:00:00");

        int result = date1.compareTo(date2);

        System.out.println("Date 1: " + date1);
        System.out.println("Date 2: " + date2);
        System.out.println("Date 1 compare to Date 2: " + result);
    }
}
The output as below.
Date 1: Sun Jul 10 07:00:00 ICT 2022
Date 2: Mon Jul 11 07:00:00 ICT 2022
Date 1 compare to Date 2: -1

The following Java example code to show the compareTo() method return 0 when the Date object is equal another Date object it compare to.

CompareDateExample2.java

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

public class CompareDateExample2 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/07/11 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/07/11 07:00:00");

        int result = date1.compareTo(date2);

        System.out.println("Date 1: " + date1);
        System.out.println("Date 2: " + date2);
        System.out.println("Date 1 compare to Date 2: " + result);
    }
}
The output as below.
Date 1: Mon Jul 11 07:00:00 ICT 2022
Date 2: Mon Jul 11 07:00:00 ICT 2022
Date 1 compare to Date 2: 0

The following Java example code to show the compareTo() method return 1 when the Date object is after another Date object it compare to.

CompareDateExample3.java

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

public class CompareDateExample3 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/07/12 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/07/11 07:00:00");

        int result = date1.compareTo(date2);

        System.out.println("Date 1: " + date1);
        System.out.println("Date 2: " + date2);
        System.out.println("Date 1 compare to Date 2: " + result);
    }
}
The output as below.
Date 1: Tue Jul 12 07:00:00 ICT 2022
Date 2: Mon Jul 11 07:00:00 ICT 2022
Date 1 compare to Date 2: 1

Compare two Date objects for Equality

In Java to compare two Date values are equal or not we can use the Date.equals() method as the following Java code.

CompareDateExample4.java

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

public class CompareDateExample4 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/04/11 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/04/11 07:00:00");
        Date date3 = simpleDateFormat.parse("2022/04/11 08:00:00");

        boolean result1 = date1.equals(date2);
        boolean result2 = date1.equals(date3);

        System.out.println("date1: " + date1);
        System.out.println("date2: " + date2);
        System.out.println("date3: " + date3);
        System.out.println("date1 is equal date2: " + result1);
        System.out.println("date1 is equal date3: " + result2);
    }
}
The output as below.
date1: Mon Apr 11 07:00:00 ICT 2022
date2: Mon Apr 11 07:00:00 ICT 2022
date3: Mon Apr 11 08:00:00 ICT 2022
date1 is equal date2: true
date1 is equal date3: false

Compare if a Date before another Date

In Java to check if a Date object is before another specified Date object we can use the Date.before() method as below.

CompareDateExample5.java

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

public class CompareDateExample5 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/07/10 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/07/11 07:00:00");
        Date date3 = simpleDateFormat.parse("2022/07/09 07:00:00");

        boolean result1 = date1.before(date2);
        boolean result2 = date1.before(date3);

        System.out.println("date1: " + date1);
        System.out.println("date2: " + date2);
        System.out.println("date3: " + date3);
        System.out.println("date1 is before date2: " + result1);
        System.out.println("date1 is before date3: " + result2);
    }
}
The output as below.
date1: Sun Jul 10 07:00:00 ICT 2022
date2: Mon Jul 11 07:00:00 ICT 2022
date3: Sat Jul 09 07:00:00 ICT 2022
date1 is before date2: true
date1 is before date3: false

Compare if a Date after another Date

In Java to check if a Date object is after another specified Date object we can use the Date.after() method as following Java code.

CompareDateExample6.java

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

public class CompareDateExample6 {
    public static void main(String... args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date1 = simpleDateFormat.parse("2022/07/10 07:00:00");
        Date date2 = simpleDateFormat.parse("2022/07/11 07:00:00");
        Date date3 = simpleDateFormat.parse("2022/07/09 07:00:00");

        boolean result1 = date1.after(date2);
        boolean result2 = date1.after(date3);

        System.out.println("date1: " + date1);
        System.out.println("date2: " + date2);
        System.out.println("date3: " + date3);
        System.out.println("date1 is after date2: " + result1);
        System.out.println("date1 is after date3: " + result2);
    }
}
The output as below.
date1: Sun Jul 10 07:00:00 ICT 2022
date2: Mon Jul 11 07:00:00 ICT 2022
date3: Sat Jul 09 07:00:00 ICT 2022
date1 is after date2: false
date1 is after date3: true

Happy Coding 😊

Java Compare two Calendar Values

Java Compare two LocalDate Values

Java Compare two LocalTime Values

Java Compare two LocalDateTime Values

Java Date by Examples

Java Calendar by Examples

Java LocalDate by Examples