Home » Java Date compareTo() Method with Examples

Java Date compareTo() Method with Examples

by Online Tutorials Library

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

  1. It returns the value 0 if the argument Date is equal to this Date.
  2. It returns a value less than 0 if this Date is before the Date argument.
  3. 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

Test it Now

Output:

Your comparison value is : -1  

Example 2

Test it Now

Output:

Your comparision value is : 0  

Example 3

Test it Now

Output:

Your comparison value is : 1  

Next Topicjava.util.Date

You may also like