03
Mar
2021

How to compare Date and Time in Java

In this article, you’ll learn several ways of comparing Dates, Times, DateTimes, and DateTimes in different TimeZones in Java.

We use the DateTime API introduced in Java 8 like LocalDate, LocalTime, LocalDateTime, ZonedDateTime, as well as older classes like Date, and Calendar to demonstrate how to compare dates.

Compare dates in Java using LocalDate

import java.time.LocalDate;

public class CompareLocalDateExample {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.now();
        LocalDate date2 = LocalDate.of(2020, 1, 31);

        // isAfter() method
        if(date1.isAfter(date2)) {
            System.out.println(date1 + " is after " + date2);
        }

        // isBefore() method
        if(date1.isBefore(date2)) {
            System.out.println(date1 + " is before " + date2);
        }

        // isEqual() method
        if(date1.isEqual(date2)) {
            System.out.println(date1 + " is equal to " + date2);
        }

        // compareTo() method
        int diff = date1.compareTo(date2);
        if(diff > 0) {
            System.out.println(date1 + " is greater than " + date2);
        } else if (diff < 0) {
            System.out.println(date1 + " is less than " + date2);
        } else {
            System.out.println(date1 + " is equal to " + date2);
        }
    }
}

Compare date and time in Java using LocalDateTime

import java.time.LocalDateTime;

public class CompareLocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime date1 = LocalDateTime.now();
        LocalDateTime date2 = LocalDateTime.of(2020, 1, 31, 10, 15, 45);

        // isAfter() method
        if(date1.isAfter(date2)) {
            System.out.println(date1 + " is after " + date2);
        }

        // isBefore() method
        if(date1.isBefore(date2)) {
            System.out.println(date1 + " is before " + date2);
        }

        // isEqual() method
        if(date1.isEqual(date2)) {
            System.out.println(date1 + " is equal to " + date2);
        }

        // compareTo() method
        int diff = date1.compareTo(date2);
        if(diff > 0) {
            System.out.println(date1 + " is greater than " + date2);
        } else if (diff < 0) {
            System.out.println(date1 + " is less than " + date2);
        } else {
            System.out.println(date1 + " is equal to " + date2);
        }
    }
}

Compare date and time with different TimeZones in Java using ZonedDateTime

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class CompareZonedDateTimeExample {
    public static void main(String[] args) {
        ZonedDateTime date1 = ZonedDateTime.of(
                LocalDateTime.of(2020, 1, 31, 10, 30, 45),
                ZoneId.of("America/New_York"));
        ZonedDateTime date2 = ZonedDateTime.of(
                LocalDateTime.of(2020, 1, 31, 10, 30, 45),
                ZoneId.of("Europe/Paris"));

        // isAfter() method
        if(date1.isAfter(date2)) {
            System.out.println(date1 + " is after " + date2);
        }

        // isBefore() method
        if(date1.isBefore(date2)) {
            System.out.println(date1 + " is before " + date2);
        }

        // isEqual() method
        if(date1.isEqual(date2)) {
            System.out.println(date1 + " is equal to " + date2);
        }

        // compareTo() method
        int diff = date1.compareTo(date2);
        if(diff > 0) {
            System.out.println(date1 + " is greater than " + date2);
        } else if (diff < 0) {
            System.out.println(date1 + " is less than " + date2);
        } else {
            System.out.println(date1 + " is equal to " + date2);
        }
    }
}

Compare time in Java using LocalTime

import java.time.LocalTime;

public class CompareLocalTimeExample {
    public static void main(String[] args) {
        LocalTime time1 = LocalTime.of(15, 20, 40);
        LocalTime time2 = LocalTime.of(10, 30, 50);

        // isAfter() method
        if(time1.isAfter(time2)) {
            System.out.println(time1 + " is after " + time2);
        }

        // isBefore() method
        if(time1.isBefore(time2)) {
            System.out.println(time1 + " is before " + time2);
        }

        // compareTo() method
        int diff = time1.compareTo(time2);
        if(diff > 0) {
            System.out.println(time1 + " is greater than " + time2);
        } else if (diff < 0) {
            System.out.println(time1 + " is less than " + time2);
        } else {
            System.out.println(time1 + " is equal to " + time2);
        }
    }
}

Compare date using Date class

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

public class CompareDateExample {
    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date1 = sdf.parse("2020-01-31 10:30:45");
        Date date2 = sdf.parse("2020-10-10 14:20:00");

        // after() method
        if(date1.after(date2)) {
            System.out.println(date1 + " is after " + date2);
        }

        // before() method
        if(date1.before(date2)) {
            System.out.println(date1 + " is before " + date2);
        }

        // equals() method
        if(date1.equals(date2)) {
            System.out.println(date1 + " is equal to " + date2);
        }

        // compareTo() method
        int diff = date1.compareTo(date2);
        if(diff > 0) {
            System.out.println(date1 + " is greater than " + date2);
        } else if (diff < 0) {
            System.out.println(date1 + " is less than " + date2);
        } else {
            System.out.println(date1 + " is equal to " + date2);
        }
    }
}

Compare date using Calendar class

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

public class CompareCalendarExample {
    public static void main(String[] args) throws ParseException {

        Calendar cal1 = Calendar.getInstance();
        cal1.set(2020, 2, 20, 10, 30, 45);

        Calendar cal2 = Calendar.getInstance();
        cal2.set(2020, 12, 18, 14, 22, 30);


        // after() method
        if(cal1.after(cal2)) {
            System.out.println(cal1 + " is after " + cal2);
        }

        // before() method
        if(cal1.before(cal2)) {
            System.out.println(cal1 + " is before " + cal2);
        }

        // equals() method
        if(cal1.equals(cal2)) {
            System.out.println(cal1 + " is equal to " + cal2);
        }

        // compareTo() method
        int diff = cal1.compareTo(cal2);
        if(diff > 0) {
            System.out.println(cal1 + " is greater than " + cal2);
        } else if (diff < 0) {
            System.out.println(cal1 + " is less than " + cal2);
        } else {
            System.out.println(cal1 + " is equal to " + cal2);
        }
    }
}
Share

Yaniv Levy

Yaniv Levy, Entrepreneur, visioner & technology passionate with over 20 years on vast experience as a Senior Software Engineer and a Software Architect.

You may also like...