How to Compare Two Dates in Java


In this example, we will learn three different methods to compare two dates in Java.

Source Code

1) Using Date.compareTo()

package com.beginner.examples;

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

public class DateCompareExample1 {

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

    	 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	 
         Date date1 = df.parse("2017-07-01 00:00:00");
         Date date2 = df.parse("2019-01-31 09:29:30");
         System.out.println("date1 : " + df.format(date1));
         System.out.println("date2 : " + df.format(date2));

         if (date1.compareTo(date2)  0) 
         {
             System.out.println("date1 is after date2");
         }
    }
}

Output:

date1 : 2017-07-01 00:00:00
date2 : 2019-01-31 09:29:30
date1 is before date2

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

package com.beginner.examples;

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

public class DateCompareExample2 {

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

    	 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	 
         Date date1 = df.parse("2017-07-01 00:00:00");
         Date date2 = df.parse("2019-01-31 09:29:30");
         System.out.println("date1 : " + df.format(date1));
         System.out.println("date2 : " + df.format(date2));

         Calendar time1 = Calendar.getInstance();
         Calendar time2 = Calendar.getInstance();
         time1.setTime(date1);
         time2.setTime(date2);
         
         if (time1.before(time2)) 
         {
             System.out.println("date1 is before date2");
         }
         if (time1.equals(time2)) 
         {
             System.out.println("date1 is equal date2");
         }
         if (time1.after(time2)) 
         {
             System.out.println("date1 is after date2");
         }

    }
}

Output:

date1 : 2017-07-01 00:00:00
date2 : 2019-01-31 09:29:30
date1 is before date2

3) Using Date.getTime()

package com.beginner.examples;

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

public class DateCompareExample3 {

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

    	 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	 
         Date date1 = df.parse("2017-07-01 00:00:00");
         Date date2 = df.parse("2019-01-31 09:29:30");
         System.out.println("date1 : " + df.format(date1));
         System.out.println("date2 : " + df.format(date2));
         
         long time1 =date1.getTime();
         long time2= date2.getTime();

         if (time1  time2) 
         {
             System.out.println("date1 is after date2");
         }
    }
}

Output:

date1 : 2017-07-01 00:00:00
date2 : 2019-01-31 09:29:30
date1 is before date2

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments