How to Compare Two Dates in Java

There are many ways to compare dates in Java. In essence, the date represents a (long) point in time and is written as the number of milliseconds that have passed since 1/1/1970. In Java, Date is an object with many comparison methods. Any method that compares two dates in essence

By compareTo . method

How to Compare Two Dates in Java Picture 1How to Compare Two Dates in Java Picture 1

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.

How to Compare Two Dates in Java Picture 2How to Compare Two Dates in Java Picture 2

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

How to Compare Two Dates in Java Picture 3How to Compare Two Dates in Java Picture 3

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

How to Compare Two Dates in Java Picture 4How to Compare Two Dates in Java Picture 4

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.

How to Compare Two Dates in Java Picture 5How to Compare Two Dates in Java Picture 5

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

How to Compare Two Dates in Java Picture 6How to Compare Two Dates in Java Picture 6

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

How to Compare Two Dates in Java Picture 7How to Compare Two Dates in Java Picture 7

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

How to Compare Two Dates in Java Picture 8How to Compare Two Dates in Java Picture 8

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.

How to Compare Two Dates in Java Picture 9How to Compare Two Dates in Java Picture 9

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);

How to Compare Two Dates in Java Picture 10How to Compare Two Dates in Java Picture 10

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

How to Compare Two Dates in Java Picture 11How to Compare Two Dates in Java Picture 11

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

How to Compare Two Dates in Java Picture 12How to Compare Two Dates in Java Picture 12

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

How to Compare Two Dates in Java Picture 13How to Compare Two Dates in Java Picture 13

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 "==".

How to Compare Two Dates in Java Picture 14How to Compare Two Dates in Java Picture 14

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

How to Compare Two Dates in Java Picture 15How to Compare Two Dates in Java Picture 15

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 

How to Compare Two Dates in Java Picture 16How to Compare Two Dates in Java Picture 16

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

How to Compare Two Dates in Java Picture 17How to Compare Two Dates in Java Picture 17

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 }
4 ★ | 1 Vote