A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/java-tutorial/util/date/java_date_compareto.php below:

Website Navigation


Java Date: compareTo() Method - w3resource

Java compareTo() MethodLast update on August 19 2022 21:51:24 (UTC/GMT +8 hours) public int compareTo(Date dt)

The compareTo() method is used to compare two dates for ordering.

Package: java.util

Java Platform: Java SE 8

Syntax:

compareTo(Date dt)

Parameters:

Name Description dt The date to be compared.

Return Value:

Return Value Type: int

Throws:
NullPointerException - if anotherDate is null.

Pictorial Presentation of Jave Date compareto() method:

Example: Java Date.compareTo(Date anotherDate) Method

import java.util.Date;
 
public class Main {
 
    public static void main(String[] args) {
 
        Date dt1 = new Date(2018, 3, 12);
        Date dt2 = new Date(2018, 5, 21);
        Date dt3 = new Date(2018, 3, 12);
 
        int output = dt1.compareTo(dt2);
        System.out.println("If date1 is after date2: " + output);
 
        output = dt2.compareTo(dt3);
        System.out.println("If date2 is before date3: " + output);
 
        output = dt1.compareTo(dt3);
        System.out.println("If date1 equal to date3: " + output);
  
    }
} 

Output:

If date1 is after date2: -1
If date2 is before date3: 1
If date1 equal to date3: 0

Java Code Editor:

Previous:clone Method
Next:equals Method




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4