In this example we are going to compare two date objects in Java programming language. For comparing the date objects we can use after() method of the java.util.Date class. The after() method returns boolean value according to date objects.
In CompareDateAfter class we have defined firstDate object and then we used the Thread.Sleep(1000) method to make thread sleep for 1 second. After one second we have created another object of date class called secondDate. Now in the program we have used after() method to compare the dates.
firstDate.after(secondDate);
Here is the full example code of CompareDateAfter class as follows:
CompareDateAfter.java
import java.util.Date;
|
Output:
C:\DateExample>javac CompareDateAfter.java C:\DateExample>java CompareDateAfter FirstDate:=Wed Oct 08 18:01:14 GMT+05:30 2008 SecondDate:=Wed Oct 08 18:01:15 GMT+05:30 2008 Second Date is initialized after First Date C:\DateExample> |
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 with the use of after method View All Comments
Post your Comment