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)); } } |
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)); } } |
true
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#equals-java.lang.Object-
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Period parse() method in Java with Examples
- Period getMonths() method in Java with Examples
- Period plusMonths() method in Java with Examples
- Period getDays() method in Java with Examples
- Period get() method in Java with Examples
- Period from() method in Java with Examples
- Period between() method in Java with Examples
- Period negated() method in Java with Examples
- Period normalized() method in Java with Examples
- Period plusYears() method in Java with Examples
- Period isNegative() method in Java with Examples
- Period hashCode() method in Java with Examples
- Period getUnits() method in Java with Examples
- Period plusDays() method in Java with Examples
- Period plus() method in Java with Examples
- Period getYears() method in Java with Examples
- Period isZero() method in Java with Examples
- Period minus() method in Java with Examples
- Period minusDays() method in Java with Examples
- Period minusMonths() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

