Compare Two Dates In Java

Chapter: Date and Time Last Updated: 23-09-2016 07:28:09 UTC

Program:

            /* ............... START ............... */
                
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class JavaCompareTwoDates {
	public static void main(String[] args) {

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = sdf.parse("2016-01-01");
			Date date2 = sdf.parse("2016-12-25");

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

			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 same as Date2");
			}

			long diff = date2.getTime() - date1.getTime();
			int days = (int) (diff / (1000 * 60 * 60 * 24));
			System.out.println("Difference: " + days + " days.");

		} catch (ParseException ex) {
		}
	}
}
                /* ............... END ............... */
        

Output

Date1: 2016-01-01
Date2: 2016-12-25
Date1 is before Date2
Difference: 359 days.

Notes:

  • Date.compareTo() is used to compare two dates in Java.
  • Return value is 0 if both dates are equal.
  • Return value is greater than 0 , if Date is after the date argument.
  • Return value is less than 0, if Date is before the date argument.

Tags

Compare Two Dates, Java

Similar Programs Chapter Last Updated
Java Format Time In 24 Hour Format Date and Time 25-10-2016
Java Month In MMM Format Date and Time 25-10-2016
Java Display Date In Different Formats Date and Time 03-09-2016
Java Display Week Number Of The Year Date and Time 03-09-2016
Java Display Time In Italian Language Date and Time 02-09-2016
Java Display Time In Different Country's Format Date and Time 02-09-2016
Java Add Time To Date Example Date and Time 02-09-2016
Java Format Seconds Example Date and Time 02-09-2016
Java Date And Time Display Example Date and Time 02-09-2016
Java Display Hour And Minute Example Date and Time 01-09-2016
Java Displaying Month Name Example Date and Time 01-09-2016
Java Program To Format Date To yyyy/MM/dd Example Date and Time 30-06-2016
Java Program To Format Date To MM/dd/yyyy Example Date and Time 30-06-2016
Java Program To Format Date To dd/MM/yyyy Example Date and Time 30-06-2016
Weekdays In Java Example Date and Time 29-04-2016
Parse String Into Date In Java Date and Time 29-04-2016
Milliseconds To Date In Java Date and Time 29-04-2016
Gregorian Calendar In Java Date and Time 29-04-2016
Add Years To Date In Java Date and Time 29-04-2016
Add Month To Date In Java Date and Time 29-04-2016
Add Hours To Date In Java Date and Time 29-04-2016
SimpleDateFormat In Java Date and Time 28-04-2016
Date Format Using SimpleDateFormat In Java Date and Time 28-04-2016
Date In Java Example Date and Time 28-04-2016
AM PM Time Using SimpleDateFormat In Java Date and Time 28-04-2016
Time Zone In Java Example Date and Time 27-04-2016
Leap Years Between Date Range In Java Date and Time 27-04-2016
Java GMT Time Example Date and Time 27-04-2016
Epoch Time In Java Date and Time 27-04-2016
Day Of Year In Java Date and Time 27-04-2016

1 2