Thursday, September 4, 2025
HomeLanguagesJavaChronoPeriod equals() method in Java with Examples

ChronoPeriod equals() method in Java with Examples

The equals() method of ChronoPeriod interface in Java is used to check if two given periods are equal or not.

The comparison is based on the type ChronoPeriod and each of the three years, months and date. To be equal, all of the three years, month and date must be individually equal.

Syntax:

boolean equals(ChronoPeriod secondChronoPeriod)

Parameters: This method accepts a single parameter secondChronoPeriod which is the another period to compare with.

Return Value: The above methods returns true if given periods are equal, else it returns false.

Below programs illustrate the above method:

Program 1:




// Java code to show the period
// equals for two given periods
  
import java.time.*;
import java.time.chrono.*;
  
public class ChronoPeriodClass {
  
    // Function to check if two given periods
    // are equals or not
    static boolean
    checkIfEqualChronoPeriod(ChronoPeriod firstChronoPeriod,
                             ChronoPeriod secondChronoPeriod)
    {
  
        return firstChronoPeriod.equals(secondChronoPeriod);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Given period
        ChronoPeriod first = Period.ofDays(28);
        ChronoPeriod second = Period.ofDays(8);
  
        System.out.println(
            checkIfEqualChronoPeriod(first, second));
    }
}


Output:

false

Program 2:




// Java code to show the period
// equals for two given periods
  
import java.time.*;
import java.time.chrono.*;
  
public class ChronoPeriodClass {
  
    // Function to check if two given periods
    // are equals or not
    static boolean
    checkIfEqualChronoPeriod(ChronoPeriod firstChronoPeriod,
                             ChronoPeriod secondChronoPeriod)
    {
  
        return firstChronoPeriod.equals(secondChronoPeriod);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Given period
        ChronoPeriod first2 = Period.ofDays(28);
        ChronoPeriod second2 = Period.ofDays(28);
  
        System.out.println(
            checkIfEqualChronoPeriod(first2, second2));
    }
}


Output:

true

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#equals-java.lang.Object-

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11796 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6715 POSTS0 COMMENTS