4 Ways to Perform Date Comparison in Java

Table of contents:

4 Ways to Perform Date Comparison in Java
4 Ways to Perform Date Comparison in Java
Anonim

There are several ways to compare two dates in the Java language. Within the program, a date is represented as an integer (long), relative to a specific point in time - the number of milliseconds that have elapsed since January 1, 1970. In this language, "Date" is an object and therefore includes various methods of comparison. Basically any method for comparing two dates actually compares two numbers that represent the instants of time to which the dates refer.

Steps

Method 1 of 4: Using the "compareTo" Method

4301351 1
4301351 1

Step 1. Use the "compareTo" method

The "Date" class implements the "Comparable" interface, so two objects of this type (ie two dates) can be directly compared via the "compareTo" method. If the dates are identical, i.e. they refer to the same instant in time, the method will return the value zero (0). If the "Date" object that invokes the "compareTo" method represents a date prior to the one used as the method argument, the comparison will return a numeric value less than zero. Conversely, if the "Date" object invoking the "compareTo" method represents a date later than the one used as an argument, the comparison will return a numeric value greater than zero. As already mentioned, if the two dates being compared are equal, the numeric value zero will be returned.

4301351 2
4301351 2

Step 2. Create two "Date" objects

The first step to take, before being able to make the comparison, is to create the two objects that will contain the dates to be compared. One way to do this is to use the "SimpleDateFormat" class. The latter allows you to insert a date into an object of type "Date" in a simple and fast way.

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); // Declaration of the object that represents the format of the date that we are going to use in the comparison. When we go to insert the values we will have to respect this format Date date1 = sdf.parse ("1995-02-23"); // date1 represents February 23, 1995 Date date2 = sdf.parse ("2001-10-31"); // date2 represents October 31, 2001 Date date3 = sdf.parse ("1995-02-23"); // date3 represents February 23, 1995

4301351 3
4301351 3

Step 3. Compare objects of type "Date"

The following code shows the results we will obtain in each of the possible cases: in the case in which the first date is less than the second, when we have two equal dates, and when the first date is greater than the second.

date1.compareTo (date2); // date1 <date2 we will get as a result a value less than 0 date2.compareTo (date1); // date2> date1 we will get as a result a value greater than 0 date1.compareTo (date3); // date1 = date3 we will get exactly 0 as a result

Method 2 of 4: Using the "Equals", "After" and "Before" Methods

4301351 4
4301351 4

Step 1. Use the "equals", "after" and "before" comparison methods

Objects of the "Date" class can be compared directly using the "equals", "after" and "before" methods. If the two dates compared refer to the same instant in time, the "equals" method will return the boolean value "true". To demonstrate the use of these methods, we will use the same example dates used to describe the behavior of the "compareTo" method.

4301351 5
4301351 5

Step 2. We compare the values using the "before" method

The following code shows both cases, ie when the boolean value "true" is returned and when "false" is returned. If "date1" represents a date earlier than that stored in the "date2" object, the "before" method will return the value "true". Otherwise we will get the boolean value "false".

System.out.print (date1.before (date2)); // the value "true" will be printed System.out.print (date2.before (date2)); // the value "false" will be printed

4301351 6
4301351 6

Step 3. We compare the values using the "after" method

The following code shows both cases, ie when the boolean value "true" is returned and when "false" is returned. If "date2" represents a date later than that stored in the "date1" object, the "after" method will return the value "true". Otherwise we will get the boolean value "false".

System.out.print (date2.after (date1)); // the value "true" will be printed System.out.print (date1.after (date2)); // the value "false" will be printed

4301351 7
4301351 7

Step 4. We compare the values using the "equals" method

The following code shows both cases, ie when the boolean value "true" is returned and when "false" is returned. If both "Date" objects of the comparison represent the same date, the "equals" method will return the value "true". Otherwise we will get the boolean value "false".

System.out.print (date1.equals (date3)); // the value "true" will be printed System.out.print (date1.equals (date2)); // the value "false" will be printed

Method 3 of 4: Using the "Calendar" Class

4301351 8
4301351 8

Step 1. Use the "Calendar" class

The latter also has the "compareTo" comparison methods: "equals", "after" and "before", which work in exactly the same way as described for the "Date" class. If the dates to be compared are stored in an object of type "Calendar", there is no reason to extract them to make the comparison, just use the methods of the object.

4301351 9
4301351 9

Step 2. Create instances of the "Calendar" class

In order to use the methods of the "Calendar" class we must first create instances of this element. Fortunately, it is possible to take advantage of the dates that we have already entered in the instances of the "Date" class.

Calendar cal1 = Calendar.getInstance (); // object declaration cal1 Calendar cal2 = Calendar.getInstance (); // object declaration cal2 Calendar cal3 = Calendar.getInstance (); // declaration of the cal3 object cal1.setTime (date1); // insert the date inside the object cal1 cal2.setTime (date2); // insert the date inside the cal2 object cal3.setTime (date3); // insert the date inside the cal3 object

4301351 10
4301351 10

Step 3. We compare the "cal1" and "cal2" objects using the "before" method

The following code will print on the screen the boolean value "true", if the date contained in "cal1" is earlier than the one stored in "cal2".

System.out.print (cal1.before (cal2)); // the value "true" will be shown on the screen

4301351 11
4301351 11

Step 4. We compare the "cal1" and "cal2" objects using the "after" method

The following code will print on the screen the boolean value "false", if the date contained in "cal1" is earlier than the one stored in "cal2".

System.out.print (cal1.after (cal2)); // the value "false" will be shown on the screen

4301351 12
4301351 12

Step 5. We compare the "cal1" and "cal2" objects using the "equals" method

The following code shows both cases, ie when the boolean value "true" will be returned and when "false" will be returned instead. The conditions for this to occur obviously depend on the value assumed by the instances of the "Calendar" class that we are going to compare. The following example code should print the value "true", followed by the value "false" on the next line.

System.out.println (cal1.equals (cal3)); // the value true will be shown since cal1 is equal to cal3 System.out.print (cal1.equals (cal2)); // the value false will be shown as cal1 is different from cal2

Method 4 of 4: Using the "getTime" Method

4301351 13
4301351 13

Step 1. Use the "getTime" method

In Java it is possible to directly compare two dates after transforming their value into a primitive data type (i.e. the language's predefined data types). The methods described above are however to be preferred, since they are more readable and may therefore be more suitable for a business context in which the source code will have to be managed by different people. Since the comparison will take place between primitive data, it can be performed directly using the comparison operators "" and "==".

4301351 14
4301351 14

Step 2. We create objects of type "long" which will contain the dates to be compared

To do this, we will have to transform the value stored in the objects of type "Date" used above into an integer of type "long". Fortunately, there is a method that does this conversion quickly and easily: "getTime ()".

    long time1 = getTime (date1); // we declare the primitive object "time1" to which we assign the value of "date1" long time2 = getTime (date2); // we declare the primitive object "time2" to which we assign the value of "date2" long time3 = getTime (date3); // we declare the primitive object "time3" to which we assign the value of "date3"

4301351 15
4301351 15

Step 3. We check if the first date is less than the second

To do this, we will use the comparison operator "<" to compare the two integer values that correspond to the dates "date1" and "date2". Since the number stored in the "time1" object is less than that present in the "time2" object, the message contained in the first branch of the "If-else" logical structure will be printed. The code block for the "else" statement has been included to respect the correctness of the syntax.

    if (time1 <time2) {System.out.println ("date1 is earlier than date2"); // this message will be printed since actually time1 is less than time2} else {System.out.println ("date1 is not older than date2"); }

4301351 16
4301351 16

Step 4. We check if the first date is greater than the second

To do this, we will use the comparison operator ">" to compare the two integer values that correspond to the dates "date1" and "date2". Since the number stored in the "time1" object is less than that present in the "time2" object, the message contained in the first branch of the "If-else" logical structure will be printed. The code block for the "else" statement has been included to respect the correctness of the syntax.

    if (time2> time1) {System.out.println ("date2 is after date1"); // this message will be printed as time2 is actually greater than time1} else {System.out.println ("date2 is not later than date1"); }

4301351 17
4301351 17

Step 5. We check if both dates are the same

To do this, we will use the comparison operator "==" to compare the two integer values that correspond to the dates "date1" and "date2". Since the number stored in the "time1" object is the same as the one in the "time3" object, the message contained in the first branch of the "If-else" logical structure will be printed. If the program were to print the second message on the screen (ie the one included in the "else" statement), it means that the two dates compared are not the same.

if (time1 == time2) {System.out.println ("The dates are the same"); } else {System.out.println ("Dates are different"); // this message will be printed as the value of time1 is actually different from time2}

Recommended: