There are various ways to compare Java dates. Internally, a date is represented as a (long) point in time -- the number of milliseconds that have elapsed since January 1 1970. In Java, Date is an object, which means it includes multiple methods for comparison. Any method of comparing two dates will essentially compare the dates' times.

Method 1
Method 1 of 4:

Using compareTo

  1. Date implements Comparable<Date> and so two dates can be compared directly with the compareTo method. If the dates are for the same point in time, the method returns zero. If the date being compared is before the date argument, a value less than zero is returned. If the date being compared is after the date argument, a value greater than zero is returned. If the dates are equal, a value of 0 is returned. [1]
  2. You will need to create each date object before you can start comparing them. One way to do this is to use the SimpleDateFormat class. It allows for easy entry of date values into date objects.
       SimpleDateFormat 
       sdf 
       = 
       new 
       SimpleDateFormat 
       ( 
       "yyyy-MM-dd" 
       ); 
       //For declaring values in new date objects. use same date format when creating dates 
       Date 
       date1 
       = 
       sdf 
       . 
       parse 
       ( 
       "1995-02-23" 
       ); 
       //date1 is February 23, 1995 
       Date 
       date2 
       = 
       sdf 
       . 
       parse 
       ( 
       "2001-10-31" 
       ); 
       //date2 is October 31, 2001 
       Date 
       date3 
       = 
       sdf 
       . 
       parse 
       ( 
       "1995-02-23" 
       ); 
       //date3 is February 23, 1995 
      
    Advertisement
  3. The code below will show you each case -- less than, equal, and greater than.
       date1 
       . 
       compareTo 
       ( 
       date2 
       ); 
       //date1 < date2, returns less than 0 
       date2 
       . 
       compareTo 
       ( 
       date1 
       ); 
       //date2 > date1, returns greater than 0 
       date1 
       . 
       compareTo 
       ( 
       date3 
       ); 
       //date1 = date3, so will print 0 to console 
      
    Advertisement
Method 2
Method 2 of 4:

Using Equals, After, and Before

  1. Dates can be compared with the equals, after and before methods. If two dates are for the same point in time, the equals method will return true. The examples will use previously created dates from the compareTo method. [2]
  2. The code below shows a true and false case. If date1 is before date2, before returns true. If it is not, before returns false.
       System 
       . 
       out 
       . 
       print 
       ( 
       date1 
       . 
       before 
       ( 
       date2 
       )); 
       //prints true 
       System 
       . 
       out 
       . 
       print 
       ( 
       date2 
       . 
       before 
       ( 
       date2 
       )); 
       //prints false 
      
  3. The code below shows a true and false case. If date2 is after date1, after returns true. If it is not, after returns false.
       System 
       . 
       out 
       . 
       print 
       ( 
       date2 
       . 
       after 
       ( 
       date1 
       )); 
       //prints true 
       System 
       . 
       out 
       . 
       print 
       ( 
       date1 
       . 
       after 
       ( 
       date2 
       )); 
       //prints false 
      
  4. The code below shows a true and false case. If the dates are equal, equals returns true. If they are not, equals returns false.
       System 
       . 
       out 
       . 
       print 
       ( 
       date1 
       . 
       equals 
       ( 
       date3 
       )); 
       //prints true 
       System 
       . 
       out 
       . 
       print 
       ( 
       date1 
       . 
       equals 
       ( 
       date2 
       )); 
       //prints false 
      
    Advertisement
Method 3
Method 3 of 4:

Using the Calendar Class

  1. The calendar class also has compareTo, equals, after and before methods that work in the same way as described above for the date class. So if the date information is being held in a calendar, there is no need to extract the date just to perform a comparison. [3]
  2. To use the Calendar methods, you will need a few Calendar instances. Luckily, you can just grab the times from the already created Date instances.
       Calendar 
       cal1 
       = 
       Calendar 
       . 
       getInstance 
       (); 
       //declares cal1 
       Calendar 
       cal2 
       = 
       Calendar 
       . 
       getInstance 
       (); 
       //declares cal2 
       Calendar 
       cal3 
       = 
       Calendar 
       . 
       getInstance 
       (); 
       //declares cal3 
       cal1 
       . 
       setTime 
       ( 
       date1 
       ); 
       //applies date to cal1 
       cal2 
       . 
       setTime 
       ( 
       date2 
       ); 
       cal3 
       . 
       setTime 
       ( 
       date3 
       ); 
      
  3. The code below should print true since cal1 is before cal2.
       System 
       . 
       out 
       . 
       print 
       ( 
       cal1 
       . 
       before 
       ( 
       cal2 
       )); 
       //will print true 
      
  4. The below code should print false since cal1 is before cal2.
       System 
       . 
       out 
       . 
       print 
       ( 
       cal1 
       . 
       after 
       ( 
       cal2 
       )); 
       //prints false 
      
  5. The below code will show an example of both a true and false case. The condition depends on the calendar instances being compared. The code should print "true," then "false" on the next line.
       System 
       . 
       out 
       . 
       println 
       ( 
       cal1 
       . 
       equals 
       ( 
       cal3 
       )); 
       //prints true: cal1 == cal3 
       System 
       . 
       out 
       . 
       print 
       ( 
       cal1 
       . 
       equals 
       ( 
       cal2 
       )); 
       //prints false: cal1 != cal2 
      
    Advertisement
Method 4
Method 4 of 4:

Using getTime

  1. It is also possible to directly compare the point of time of two dates, although any of the previous approaches are likely to be more readable and so preferable. This will be a comparison of two primitive data types, so it can be done with "<", ">", and "==".
  2. Before you can compare the dates, you must create long integers with the data from the previously created Date objects. Luckily, the getTime() method will do most of the work for you.
       long 
       time1 
       = 
       getTime 
       ( 
       date1 
       ); 
       //declares primitive time1 from date1 
       long 
       time2 
       = 
       getTime 
       ( 
       date2 
       ); 
       //declares primitive time2 from date2 
      
  3. Use the less than symbol (<) to compare these two integer values. Since time1 is less than time 2, the first message should print. The else statement is included for proper syntax.
       if 
       ( 
       time1 
       < 
       time2 
       ){ 
       System 
       . 
       out 
       . 
       println 
       ( 
       "date1 is before date2" 
       ); 
       //will print since time1 <time2 
       } 
       else 
       { 
       System 
       . 
       out 
       . 
       println 
       ( 
       "date1 is not before date2" 
       ); 
       } 
      
  4. Use the greater than symbol (>) to compare these two integer values. Since time1 is greater than time 2, the first message should print. The else statement is included for proper syntax.
       if 
       ( 
       time2 
       > 
       time1 
       ){ 
       System 
       . 
       out 
       . 
       println 
       ( 
       "date2 is after date1" 
       ); 
       //will print since time2 > time1 
       } 
       else 
       { 
       System 
       . 
       out 
       . 
       println 
       ( 
       "date2 is not after date1" 
       ); 
       } 
      
  5. Use the symbol to check for equality (==) to compare these two integer values for equality. Since time1 is equal to time3, the first message should print. If the program gets to the else statement, that means the times are not equal. [4]
       if 
       ( 
       time1 
       == 
       time2 
       ){ 
       System 
       . 
       out 
       . 
       println 
       ( 
       "the dates are equal" 
       ); 
       } 
       else 
       { 
       System 
       . 
       out 
       . 
       println 
       ( 
       "the dates are not equal" 
       ); 
       //will print since time1 != time2 
       } 
      
    Advertisement

Expert Q&A

Ask a Question

      Advertisement

      Video

      About this article

      Thanks to all authors for creating a page that has been read 150,374 times.

      Is this article up to date?

      Advertisement