4 ways to compare two dates in Java

Table of contents:

4 ways to compare two dates in Java
4 ways to compare two dates in Java
Anonim

There are different ways to compare dates in Java. In this computer language, a date is represented as a point (of type long) in time, representing the number of milliseconds since January 1, 1970. In Java, Date is an object, which means that it includes several methods comparison. Any method comparing two dates will consist primarily of comparing the times of those dates.

Steps

Method 1 of 4: Using compareTo

4301351 1

Step 1. Use compareTo

The Date object implements Comparable and so two dates can be compared directly using the compareTo method. If the dates match the same point in time, the method will return zero. If the compared date is before the date given in argument, a negative value will be returned. If the compared date is after the date given in argument, a positive value will be returned. If the dates are equal, the returned value will be 0.

4301351 2

Step 2. Create the date objects

You will need to create each Date object before you start comparing them. One way to do this is to use the SimpleDateFormat class. It allows you to easily enter date values ​​into date objects.

    SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); // To declare values ​​in new Date objects, use the same date format as for creating dates Date date1 = sdf.parse ("1995-02-23"); // The date1 is February 23, 1995 Date date2 = sdf.parse ("2001-10-31"); // Date2 is October 31, 2001 Date date3 = sdf.parse ("1995-02-23"); // The date3 is February 23, 1995

4301351 3

Step 3. Compare the Date objects

The code below will show you each case: lesser, greater, equal.

    date1.compareTo (date2); // date1 <date2, return a negative date2.compareTo (date1); // date2> date1, return a positive date1.compareTo (date3); // date1 = date3, the console will display 0

Method 2 of 4: Using the equals, after, and before methods

4301351 4

Step 1. Use equals, after and before

Dates can be compared using the equals, after, and before methods. If two dates match the same time point, the equals method will return true. The following examples will use the dates previously created for the compareTo method.

4301351 5

Step 2. Compare with the before method

The code given below shows a true case and a false case. If date1 is before date2, before returns true. If not, it returns false.

    System.out.print (date1.before (date2)); // display true System.out.print (date2.before (date2)); // displays false

4301351 6

Step 3. Compare with the after method

The code given below shows a true case and a false case. If date1 is after date2, after returns true. If not, after returns false.

    System.out.print (date2.after (date1)); // displays true System.out.print (date1.after (date2)); // displays false

4301351 7

Step 4. Make a comparison with the equals method

The code given below shows a true case and a false case. If the dates are equal, equals returns true. If not, equals returns false.

    System.out.print (date1.equals (date3)); // displays true System.out.print (date1.equals (date2)); // displays false

Method 3 of 4: Use the Calendar class

4301351 8

Step 1. Use the calendar

The Calendar class also has the compareTo, equals, after, and before methods, which work the same as described above for the Date class. Thus, if the date information is contained in a calendar, there is no need to extract the date just to make a comparison.

4301351 9

Step 2. Create instances of Calendar

To use the methods of the Calendar class, you will need a few instances of Calendar. Fortunately, you can retrieve time information from already created Date instances.

    Calendar cal1 = Calendar.getInstance (); // declare cal1 Calendar cal2 = Calendar.getInstance (); // declare cal2 Calendar cal3 = Calendar.getInstance (); // declare cal3 cal1.setTime (date1); // enter the date in cal1 cal2.setTime (date2); cal3.setTime (date3);

4301351 10

Step 3. Compare cal1 and cal2 using a before method

Since cal1 is before cal2, the code below should display true.

    System.out.print (cal1.before (cal2)); // will display true

4301351 11

Step 4. Compare cal1 and cal2 using the after method

Since cal1 is before cal2, the code below should display false.

    System.out.print (cal1.after (cal2)); // make display false

4301351 12

Step 5. Compare cal1 and cal2 using the equals method

The code given below will show an example of the true case and an example of the false case. The condition depends on the Calendar instances that are being compared. This code should display "true" followed by "false" on the next line.

    System.out.println (cal1.equals (cal3)); // display true: cal1 == cal3 System.out.print (cal1.equals (cal2)); // display false: cal1! = cal2

Method 4 of 4: Use the getTime method

4301351 13

Step 1. Use getTime

It is also possible to directly compare the time point of the two dates, although any of the approaches given previously should be more readable and therefore preferable. This will be a comparison between two types of primitive data, so it can be done with “” and “==”.

4301351 14

Step 2. Create the long type time objects

Before you can compare dates, you need to create long integers (or "long integers") with data from previously created Date objects. Luckily, the getTime () method will do most of the work for you.

    long time1 = getTime (date1); // declare the primitive time1 starting from date1 long time2 = getTime (date2); // declare the primitive time2 from date2

4301351 15

Step 3. Make a “less than” comparison

Use the “less than” (<) symbol to compare two integer values. As time1 is less than time 2, the first message should be displayed. The "else" condition is included for correct syntax.

    if (time1 <time2) {System.out.println ("date1 is before date2"); // will be displayed because time1 <time2} else {System.out.println ("date1 is not before date2"); }

4301351 16

Step 4. Make a “more than” comparison

Use the more than symbol (<) to compare two integer values. As time1 is greater than time 2, the first message should be displayed. The "else" condition is included for correct syntax.

    if (time2> time1) {System.out.println ("date2 is after date1"); // will be displayed because time2> time1} else {System.out.println ("date2 is not after date1"); }

4301351 17

Step 5. Make an equality comparison

Use the equality check symbol (==) to compare two integer values ​​and see if they are equal. Since time1 is equal to time 3, the first message should be displayed. If the code passes the else condition, it means that the times are not equal.

    if (time1 == time2) {System.out.println ("dates are equal"); } else {System.out.println ("dates are not equal"); // will be displayed because time1! = time2}

Popular by topic