How to Compare Two Dates in Java? Check If They Are Equal, Later or Earlier

There are multiple ways to compare two dates in Java, it also depends on upon what exactly comparison means. If you are looking to compare two dates to find out whether they are equal or not, then you can simply use equals() method of java.util.Date class. This method will return true if both dates are equal, precisely both thave same millisecond value. If you are looking to find out whether a date comes before or after another date then you have 3 choices, you can use compareTo() method of java.util.Date class, or you can use before() and after() method of Date class, or you can use before() and after() method of Calendar class. I suggest leveraging before() and after() method of Date class, they are more readable and easy to use.

Java - InstanceOf KeyWord Example and Tutorial

The Java programming language and JVM are full of hidden gems and even though I am using Java for more than a decade I still get surprised of features which I didn't know for quite some time e.g. shutdown hook, covariant method overriding, and JVM option to refresh DNS cache.  The instanceof operator is also one of the rarely known features of Java, It is used to check if an object is the instance of a particular Class or not. It returns true if the object is an instance of the class, otherwise, returns false. You might have seen usages of instanceof operator in Java while overriding equals() method. Since for checking equality of two instances, the first step is to verify whether they are the instance of the same object or not, you can use the instanceof operator there.