In this example we are going to compare two date objects using java.util.Date class. For comparing dates, we will be using compareTo() method. The compareTo() method returns integer value after comparing the dates.
In CompareDate example class we have defined firstDate object and then we used the Thread.Sleep(1000) method to make thread to sleep for 1 second and then it creates new object secondDate after. So that second date is created after one second of creation of firstDate. Now in our example program we can compare these two dates using compareTo() method.
firstDate.compareDate(secondDate);
Here is the full example code of CompareDate class as follows:
CompareDate.java
import java.util.Date;
|
Output:
| C:\DateExample>javac CompareDate.java C:\DateExample>java CompareDate FirstDate:=Wed Oct 08 17:22:05 GMT+05:30 2008 SecondDate:=Wed Oct 08 17:22:06 GMT+05:30 2008 Second Date is initialized after First Date |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Comparing two Dates in Java View All Comments
Post your Comment