Thursday, February 5, 2026
HomeLanguagesJavaYear equals() method in Java with Examples

Year equals() method in Java with Examples

The equals() method of Year class in Java is used to check if this Year object is equals to another Year object passes as a parameter. The comparison is based on the time-line position of the years.

Syntax:

public boolean equals(Object obj)

Parameter: This method accepts a single parameter obj. It is the Year object which specifies a year with which we want to compare the current year object.

Return Value: It returns a boolean True value if the current Year object is found to be equal to the Year object passed as a parameter to it, otherwise false.

Below programs illustrate the equals() method of Year in Java:
Program 1:




// Program to illustrate the equals() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Creates first Year object
        Year firstYear = Year.of(2017);
  
        // Creates second year object
        Year secondYear = Year.of(2018);
  
        // Checks if the two year objects are
        // equal or not
        System.out.println(firstYear.equals(secondYear));
    }
}


Output:

false

Program 2:




// Program to illustrate the equals() method
  
import java.util.*;
import java.time.*;
  
public class GfG {
    public static void main(String[] args)
    {
        // Creates first Year object
        Year firstYear = Year.of(2018);
  
        // Creates second year object
        Year secondYear = Year.of(2018);
  
        // Checks if the two year objects are
        // equal or not
        System.out.println(firstYear.equals(secondYear));
    }
}


Output:

true

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32487 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6861 POSTS0 COMMENTS
Nicole Veronica
11983 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12071 POSTS0 COMMENTS
Shaida Kate Naidoo
6994 POSTS0 COMMENTS
Ted Musemwa
7233 POSTS0 COMMENTS
Thapelo Manthata
6945 POSTS0 COMMENTS
Umr Jansen
6926 POSTS0 COMMENTS