94
Java Date compareTo() Method
The compareTo() method of Java Date class compares two dates and sort them for order. This method is specified by compareTo in interface Comparable<Date>.
Syntax:
Parameters
anotherDate – a date to be compared
Return
- It returns the value 0 if the argument Date is equal to this Date.
- It returns a value less than 0 if this Date is before the Date argument.
- It returns a value greater than 0 if this Date is after the Date argument.
Exception
NullPointerException will be thrown if anotherDate is null.
Example 1
Output:
Your comparison value is : -1
Example 2
Output:
Your comparision value is : 0
Example 3
Output:
Your comparison value is : 1
Next Topicjava.util.Date