Compare dates in Java

Author: Roger Morrison
Date Of Creation: 24 September 2021
Update Date: 1 July 2024
Anonim
How to Compare two Dates in Java using compareTo() method
Video: How to Compare two Dates in Java using compareTo() method

Content

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, which means that it contains multiple methods for comparing dates. Any method of comparing two dates will basically compare the elapsed time of both dates.

To step

Method 1 of 4: Using the "compareTo" command

  1. Use compareTo. Date implements ComparableDate> and so two dates can be directly compared with each other using the compareTo method. If the dates indicate the same moment in time, the method will return zero. If the date being compared is before the date argument, then a negative value will be returned. If the date being compared is a later date than that of the date argument, then a positive value will be returned. If the dates are the same, a zero will be returned.
  2. Create the Date objects. You will have to create each date object before you can start comparing them. One way to do this is to apply the SimpleDateFormat class. This allows you to easily enter dates into Date objects.

      SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd"); // For declaring values ​​in new date objects.Use the same date format when creating dates Date date1 = 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

  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 methods "equals, after and before"

  1. Use equals, after and before. Dates can be compared with each other 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.
  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

  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

  4. Compare using the equals method. The code below shows a case of true and false. If the dates are the same, equals returns true. If 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

  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.
  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 (); // declares cal1 Calendar cal2 = Calendar.getInstance (); // declares cal2 Calendar cal3 = Calendar.getInstance (); // declares cal3 cal1.setTime (date1); // applies date to cal1 cal2.setTime (date2); cal3.setTime (date3);

  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

  4. Compare cal1 and cal2 using after. The code below returns false, because cal1 is earlier than cal2.

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

  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

  1. Use getTime. It is also possible to directly compare two time points, although any of the foregoing approaches is likely to yield more readable results and is thus preferred. This is a comparison of two primitive data types, so can be done with "", ">" and "==".
  2. Create the "long" time objects. Before you can compare dates, you must 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 of date1 long time2 = getTime (date2); // declares primitive time2 of date2

  3. Use a "less than" equation. Use a "less than" symbol () to compare these two integer values. Since time1 is less 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"); }

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

  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 gets to the else statement, it means that the times are not the same.

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