There are multiple methods to compare dates in Java. Essentially, a date represents a specific moment in time, expressed as the number of milliseconds elapsed since January 1, 1970. In Java, the Date object comes with various comparison methods. Any method that compares two dates fundamentally evaluates their respective timestamps.
Steps to Compare Dates in Java
Using the compareTo Method

The compareTo method is a straightforward way to compare dates. Since the Date class implements the Comparable interface, two dates can be directly compared using this method. If the dates are identical, the method returns 0. If the date being compared is earlier, it returns a value less than 0. If the date is later, it returns a value greater than 0. A return value of 0 indicates that the dates are equal.

Create date objects. Before comparing dates, you need to instantiate each date object. One effective way is to use the SimpleDateFormat class, which simplifies parsing date values into date objects.

Compare date objects. The following code demonstrates scenarios for less than, equal to, and greater than comparisons.
Using equals, after, and before methods

Utilize equals, after, and before methods. These methods allow you to compare dates effectively. The equals method returns true if two dates represent the same moment. The examples below use the dates created earlier in the compareTo method.

Compare using the before method. This code illustrates two cases: true and false. If date1 is earlier than date2, the before method returns true; otherwise, it returns false.

Compare using the after method. This code demonstrates two outcomes: true and false. If date2 is later than date1, the after method returns true; otherwise, it returns false.

Compare using the equals method. This code showcases two scenarios: true and false. If the two dates represent the same moment, equals returns true; otherwise, it returns false.
Using the Calendar class

Utilize the Calendar class. The Calendar class also includes compareTo, equals, after, and before methods, which function similarly to those in the Date class. If your date information is stored in a calendar, there's no need to extract it solely for comparison.

Create Calendar instances. To use calendar methods, you'll need Calendar instances. Fortunately, you can simply retrieve the time from pre-existing Date instances.

Compare cal1 and cal2 using the before method. The code below will return true because cal1 precedes cal2.

Compare cal1 and cal2 using the after method. The code below will return false since cal1 occurs before cal2.

Compare cal1 and cal2 using the equals method. The code below demonstrates both true and false scenarios. The outcome depends on the calendar instances being compared. This code will return "true" followed by "false" in the next line.
Using the getTime method

Utilize the getTime method. This method can also directly compare the timestamps of two dates, though the above methods are more readable and preferred. This comparison involves primitive data types, allowing the use of "<", ">", and "==" operators.

Creating a long time object. Before comparing dates, you need to generate a long integer from the previously created Date objects. Fortunately, the getTime() method handles most of the work for you.
long time1 = getTime(date1); //represents the primitive time1 from date1 long time2 = getTime(date2); //represents the primitive time2 from date2

Performing a less-than comparison. Use the less-than symbol (<) to compare the two long integer values. Since time1 precedes time2, the first result will be displayed. The remaining statement is included for proper syntax.
if(time1 < time2){ System.out.println("date1 is before date2"); //this result will display because time1 < time2 } else{ System.out.println("date1 is not before date2"); }

Applying a greater-than comparison. Use the greater-than symbol (>) to compare the two long integer values. Since time1 is greater than time2, the first result will be returned. The remaining statement is included for proper syntax.
if(time2 > time1){ System.out.println("date2 is after date1"); //this result will display because time2 > time1 } else{ System.out.println("date2 is not after date1"); }

Executing an equality comparison. Use the equality operator (==) to check if the two long integer values are equal. Since time1 equals time3, the first result will be returned. If the program returns a different statement, it means the times are not equal.
if(time1 == time2){ System.out.println("the dates are equal"); } else{ System.out.println("the dates are not equal"); //this result will display because time1 != time2 }