views
Using compareTo
Use compareTo. Date implements Comparable
Create the date objects. You will need to create each date object before you can start comparing them. One way to do this is to use the SimpleDateFormat class. It allows for easy entry of date values into date objects. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //For declaring values in new date objects. use 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
Compare the date objects. The code below will show you each case -- less than, equal, and greater than. date1.compareTo(date2); //date1 < date2, returns less than 0 date2.compareTo(date1); //date2 > date1, returns greater than 0 date1.compareTo(date3); //date1 = date3, so will print 0 to console
Using Equals, After, and Before
Use equals, after and before. Dates can be compared with the equals, after and before methods. If two dates are for the same point in time, the equals method will return true. The examples will use previously created dates from the compareTo method.
Compare using the before method. The code below shows a true and false case. If date1 is before date2, before returns true. If it is not, before returns false. System.out.print(date1.before(date2)); //prints true System.out.print(date2.before(date2)); //prints false
Compare using the after method. The code below shows a true and false case. If date2 is after date1, after returns true. If it is not, after returns false. System.out.print(date2.after(date1));//prints true System.out.print(date1.after(date2));//prints false
Compare using the equals method. The code below shows a true and false case. If the dates are equal, equals returns true. If they are not, equals returns false. System.out.print(date1.equals(date3));//prints true System.out.print(date1.equals(date2));//prints false
Using the Calendar Class
Use the calendar. The calendar class also has compareTo, equals, after and before methods that work in the same way as described above for the date class. So if the date information is being held in a calendar, there is no need to extract the date just to perform a comparison.
Create instances of Calendar. To use the Calendar methods, you will need a few Calendar instances. Luckily, you can just grab the times from the already created 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);
Compare cal1 and cal2 using before. The code below should print true since cal1 is before cal2. System.out.print(cal1.before(cal2)); //will print true
Compare cal1 and cal2 using after. The below code should print false since cal1 is before cal2. System.out.print(cal1.after(cal2)); //prints false
Compare cal1 and cal2 using equals. The below code will show an example of both a true and false case. The condition depends on the calendar instances being compared. The code should print "true," then "false" on the next line. System.out.println(cal1.equals(cal3)); //prints true: cal1 == cal3 System.out.print(cal1.equals(cal2)); //prints false: cal1 != cal2
Using getTime
Use getTime. It is also possible to directly compare the point of time of two dates, although any of the previous approaches are likely to be more readable and so preferable. This will be a comparison of two primitive data types, so it can be done with "<", ">", and "==".
Create the long time objects. Before you can compare the dates, you must create long integers with the data from the previously created Date objects. Luckily, 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
Do a less than comparison. Use the less than symbol (<) to compare these two integer values. Since time1 is less than time 2, the first message should print. The else statement is included for proper syntax.
if(time1 < time2){
System.out.println("date1 is before date2"); //will print since time1 Do a greater than comparison. Use the greater than symbol (>) to compare these two integer values. Since time1 is greater than time 2, the first message should print. The else statement is included for proper syntax.
if(time2 > time1){
System.out.println("date2 is after date1"); //will print since time2 > time1
}
else{
System.out.println("date2 is not after date1");
} Do an equals comparison. Use the symbol to check for equality (==) to compare these two integer values for equality. Since time1 is equal to time3, the first message should print. If the program gets to the else statement, that means the times are not equal.
if(time1 == time2){
System.out.println("the dates are equal");
}
else{
System.out.println("the dates are not equal"); //will print since time1 != time2
}
Comments
0 comment