Compare dates in Java

Table of contents:

Compare dates in Java
Compare dates in Java
Anonim

There are several ways to compare dates in Java. Internally, a date is represented as a (long) point in time – the number of milliseconds that have passed since January 1, 1970. In Java, date is the Date object, meaning it includes multiple methods (methods) for comparing dates. Any method of comparing two dates will basically compare the elapsed time of both dates.

Steps

Method 1 of 4: Using the 'compareTo' command

4301351 1

Step 1. Use compareTo

Date implements Comparable and thus two dates can be directly compared with each other using the compareTo method. If the dates indicate the same point in time, the method will return zero. If the date being compared to is before the date argument, then a negative value will be returned. If the date being compared to is a later date than the date argument, then a positive value will be returned. If the dates are the same, a zero will be returned.

4301351 2

Step 2. Create the Date objects

You will have to create each date object before you can compare them. One way to do this is to apply the SimpleDateFormat class. It allows you to easily enter dates in Date objects.

    SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd'); //For declaring values ​​in new date objects. Use the same date format when creating dates Datedatum1 = sdf.parse('1995-02-23'); //date1 is February 23, 1995 Date date2 = sdf.parse('2001-10-31'); //date2 is October 31, 2001 Date date3 = sdf.parse('1995-02-23'); //date3 is February 23, 1995

4301351 3

Step 3. Compare the date objects

The below shows each case – less than, equal to, and greater than.

    date1.compareTo(date2); //date1 < date2, less than 0 date2.compareTo(date1); //date2 > date1, returns greater than 0 date1.compareTo(date3); //date1 = date3, returns 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 indicate the same time, the equals method will return 'true'. The examples use the previously created dates via the compareTo method.

4301351 5

Step 2. Compare using the before method

The code below shows a case of true and false. If date1 is earlier than date2, the result is true. If not, before returns false.

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

4301351 6

Step 3. Compare this with the after method

The code below shows a case of true and false. If date2 is later than date1, then after returns true. If not, after returns false.

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

4301351 7

Step 4. Compare using the equals method

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

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

Method 3 of 4: Using the Calendar class

4301351 8

Step 1. Use the Calendar class

The Calendar class also has the compareTo, equals, after, and before methods, which work in the same way as described above for the date class. So if the date data is kept in a calendar, there is no need to extract 'date', just to compare two dates.

4301351 9

Step 2. Create Instance of Calendar

To use the Calendar methods, you need some Calendar instances. Fortunately, you can use the time as generated by the Date instances.

    Calendar cal1 = Calendar.getInstance(); //declare cal1 Calendar cal2 = Calendar.getInstance(); //declare cal2 Calendar cal3 = Calendar.getInstance(); //declare cal3 cal1.setTime(date1); //applies date to cal1 cal2.setTime(date2); cal3.setTime(date3);

4301351 10

Step 3. Compare cal1 and cal2 using before

The code below returns true because cal1 is earlier than cal2.

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

4301351 11

Step 4. Compare cal1 and cal2 using after

The code below returns false because cal1 is before cal2.

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

4301351 12

Step 5. Compare cal1 and cal2 using equals

The code below shows an example of both true and false. The condition depends on the Calendar instances being compared. The code below returns 'true' and then 'false' on the next line.

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

Method 4 of 4: Using the 'getTime' method

4301351 13

Step 1. Use getTime

It is also possible to directly compare two time points, although any of the foregoing approaches are likely to yield more readable results and thus are preferred. This is a comparison of two primitive data types, so can be done with '' and '=='.

4301351 14

Step 2. Create the 'long' time objects

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

    long time1 = getTime(date1); //declares primitive time1 from date1 long time2 = getTime(date2); //declares primitive time2 from date2

4301351 15

Step 3. Use a "less than" equation

Use a less-than symbol (<) to compare these two integer values. Since time1 is smaller than time2, the first message should be printed on the screen. The else statement is included for the correct syntax.

    if(time1 < time2){ System.out.println('date1 is earlier than date2'); //print because time1 < time2 } else{ System.out.println('date1 is later than or equal to date2'); }

4301351 16

Step 4. Do a 'greater than' comparison

Use the greater than symbol (>) to compare these two integers. Since time1 is greater than time2, the first message is printed on the screen. The else statement is included for correct syntax.

    if(time2 > time1){ System.out.println('date2 comes after date1'); // print because time2 > time1 } else{ System.out.println('date2 is earlier than or equal to date1'); }

4301351 17

Step 5. Do an 'equal to' comparison

Use the symbol (==) to compare these two integers. Since time1 equals time3, the first message should be printed. If the program arrives at the else statement, this means that the times are not equal.

    if(time1 == time2){ System.out.println('The dates are the same'); } else{ System.out.println('The dates are not equal'); //print because time1 != time2 }

Popular by topic