In this section, we will show you how to compare two given dates.
Comparing Dates in Java
This example illustrates you how to compare two dates. Generally dates are calculated in milliseconds. In this section, we will show you how to compare two given dates. To compare dates the given program is helpful for application development. You can easily use the code for comparing dates.
Program Description:
This program describes how the dates are compared. Here, date is mentioned in the code which has to be compared from the current date. It will show the result whether the current date is greater, equal or less than the given date.
Code Description:
Some built-in methods, fields and APIs are explained as follows:
Calendar.getInstance():
Usually this provides the instance of the Calendar class. It returns
the calendar for the default time zone and locale.
Calendar.set(2000, Calendar.JUNE, 29):
This method sets the default date to the calendar passing three arguments
like year, month and day for the specific object of the Calendar class.
cal.before(currentcal):
Usually this method returns a boolean value true, if the date
mentioned for the cal object is previous date of the mentioned date for
the currentcal object of the Calendar class, otherwise it returns
the boolean value false.
cal.after(currentcal):
Usually this method returns a booean value true, if the date
mentioned for the cal object is later date from the mentioned date for
the currentcal object of the Calendar class, otherwise it returns
the boolean value false.
new SimpleDateFormat("dd/MM/yyyy"):
Above creates the new instance of the SimpleDateFormat class of the java.text.*;
package. It specifies the format for the date. The format is mentioned in the
constructor of the SimpleDateFormat class which is the String type argument.
SimpleDateFormat.format(date):
It formats the date in the specified format.
Calendar.getTime():
This method returns the default date in time.
Here is the code of the program:
import java.util.*;
|