How to Compare Two Dates in Java
By compareTo . method
Use compareTo method. Date is a comparable successor of and so two dates can be compared directly using the compareTo method. If these dates have the same time, the method will return 0. If the date to be compared is before the other date, a value less than 0 will be returned. If the date to be compared is after the other date, the return value will be greater than 0. If the two dates are equal, the value 0 will be returned.
Create a date object. You will need to create each date object before you start comparing them. One of the ways to do this is to use the SimpleDateFormat class. This class allows entering date values into date object easily.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //To declare new values in a new date object, use the same format when creating the date. Date date1 = sdf.parse("1995-02-23"); //date1 is 23/2/1995 Date date2 = sdf.parse("2001-10-31"); //date2 is 31/10/2001 Date date3 = sdf.parse("1995-02-23"); //date3 is 23/2/1995
Compare date objects. The code below will show you each case: less than, equal to and more than.
date1.compareTo(date2); //date1 < date2, return value less than 0 date2.compareTo(date1); //date2 > date1, return value greater than 0 date1.compareTo(date3); //date1 = date3, display value 0
By equals, after and before . methods
Use equals, after and before methods. You can compare dates using equals, after and before methods. If two dates have the same time, the equals method will return true. The examples below will use the date generated from the previous compareTo method.
Compare using the before method. This code has two cases: true and false. If date1 is before date2, before will return true. Otherwise, before will return false.
System.out.print(date1.before(date2)); //return true System.out.print(date2.before(date2)); //return false value
Compare using the after method. This code has two cases: true and false. If date2 is present after date1, after will return true. Otherwise, after will display false value.
System.out.print(date2.after(date1)); //return true System.out.print(date1.after(date2)); //return false
Compare using equals method. This code has two cases: true and false. If these two dates are at the same time, equals will return true. Otherwise, equals will return false.
System.out.print(date1.equals(date3)); //returns true System.out.print(date1.equals(date2)); //returns false
By class calendar
Use a calendar (calendar). The calendar class also has compareTo, equals, after and before methods, which work similarly to the date class described above. So if the date information is in the calendar then you don't need to extract the date just for comparison.
Create a case in Calendar. To use the methods in the calendar, you'll need some instance of Calendar. Luckily you only need to get the time from the built-in Date instances.
Calendar cal1 = Calendar.getInstance(); //indicates cal1 Calendar cal2 = Calendar.getInstance(); //indicates cal2 Calendar cal3 = Calendar.getInstance(); //indicates as cal3 cal1.setTime(date1); //apply date to cal1 cal2.setTime(date2); cal3.setTime(date3);
Compare cal1 and cal2 using the before method. The code below will return true because cal1 is before cal2.
System.out.print(cal1.before(cal2)); //return true
Compare cal1 and cal2 using the after method. The code below will return false because cal1 is before cal2.
System.out.print(cal1.after(cal2)); //return false value
Compare cal1 and cal2 using the equals method. The code below will show examples of both true and false. The condition depends on the case where the calendar is compared. This code will return "true", then "false" on the next line.
System.out.println(cal1.equals(cal3)); //return true: cal1 == cal3 System.out.print(cal1.equals(cal2)); //return false: cal1 != cal2
By method getTime
Use getTime method. This method can also compare two dates directly, but the methods above are easier to read and prefer. This is a comparison between two primitive data types, so can return "<", ">" and "==".
Create a long time object. Before comparing dates, you need to create a long integer of type long that takes data from previously created Date objects. Luckily the getTime() method will do most of the work for you.
long time1 = getTime(date1); //represent primitive time1 from date1 long time2 = getTime(date2); //represent primitive time2 from date2
Do the smaller comparison. Use less than sign (<) to compare two integer values. Because time1 precedes time 2, the first result will be displayed. The remaining statement will be included for proper syntax.
if(time1 < time2){ System.out.println("date1 is before date2"); //this result will be displayed because time1
Apply the greater than comparison. Use the greater than sign (>) to compare the two integer values above. Because time1 is greater than time 2, the first result will be returned. The remaining statement will be included for proper syntax.
if(time2 > time1){ System.out.println("date2 is after date1"); //this result will be visible because time2 > time1 } else{ System.out.println("date2 is not after date1"); }
Do the equality comparison. Use the equality check mark (==) to compare if two integer values are equal or not. Because time1 is equal to time3, the first result will be returned. If the program receives a different statement, it means that 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 be visible because time1 != time2 }
You should read it
- How to Compare Two Dates in Java
- What is Java? Why choose Java?
- Basic Java syntax
- What is JAVA file? How to open, edit and convert JAVA files
- eQuiz - Multiple choice quiz about JAVA
- Download and install Java on the computer
- Basic Java exercises, with sample decoding
- Which career Java programming options are waiting for you?
- How to format dates in Excel
- How to install Java on a Raspberry Pi
- How to fix the error does not install Java
- How to Tune a Java Virtual Machine (JVM)
Maybe you are interested
How to Fix Clipboard History Error in Windows 11 Latest Update
4 Ways to update Win 11 Driver, quick update tips
Microsoft makes major improvements to Windows Update, updates will be installed much faster
WinRAR 6.10 beta update fixes Windows 11 context menu bug
Windows updates constantly, why?
How to update graphics drivers in Windows 10 and 11