Java Date Time - YearMonth compareTo(YearMonth other) example








YearMonth compareTo(YearMonth other) compares this year-month to another year-month.

Syntax

compareTo has the following syntax.

public int compareTo(YearMonth other)

Example

The following example shows how to use compareTo.

import java.time.YearMonth;

public class Main {
  public static void main(String[] args) {
    YearMonth y = YearMonth.now();
    System.out.println(y.compareTo(YearMonth.now()));

  }
}

The code above generates the following result.