Friday, September 5, 2025
HomeLanguagesJavaPeriod equals() method in Java with Examples

Period equals() method in Java with Examples

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

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

Syntax:

public boolean equals(Period secondPeriod)

Parameters: This method accepts a single parameter secondPeriod 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.LocalDate;
import java.time.Period;
  
public class PeriodClass {
  
    // Function to check if two given periods
    // are equals or not
    static boolean checkIfEqualPeriod(Period firstPeriod,
                                      Period secondPeriod)
    {
  
        return firstPeriod.equals(secondPeriod);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Given period
        Period first = Period.ofDays(28);
        Period second = Period.ofDays(8);
  
        System.out.println(checkIfEqualPeriod(first, second));
    }
}


Output:

false

Program 2:




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


Output:

true

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#equals-java.lang.Object-

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS