Thursday, June 11, 2026
HomeLanguagesJavaWeekFields equals() method in Java with Examples

WeekFields equals() method in Java with Examples

The equals() method of WeekFields class is used to compare if this WeekFields is equal to the specified object which was passed as a parameter. The comparison is based on the entire state of the rules, which is the first day-of-week and minimal days.

Syntax:

public boolean equals(Object object)

Parameters: This method accepts object which is the other rules to compare to, null returns false.

Return value: This method returns true if this is equal to the specified rules.

Below programs illustrate the WeekFields.equals() method:
Program 1:




// Java program to demonstrate
// WeekFields.equals() method
  
import java.time.DayOfWeek;
import java.time.temporal.WeekFields;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create WeekFields
        WeekFields weekFields
            = WeekFields.of(DayOfWeek.MONDAY, 1);
        WeekFields otherWeekFields
            = WeekFields.of(DayOfWeek.MONDAY, 1);
  
        // apply equals()
        boolean bothAreEquals
            = weekFields.equals(otherWeekFields);
  
        // print results
        System.out.println("Equals: "
                           + bothAreEquals);
    }
}


Output:

Equals: true

Program 2:




// Java program to demonstrate
// WeekFields.equals() method
  
import java.time.DayOfWeek;
import java.time.temporal.WeekFields;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create WeekFields
        WeekFields weekFields
            = WeekFields.of(DayOfWeek.MONDAY, 1);
        WeekFields otherWeekFields
            = WeekFields.of(DayOfWeek.SUNDAY, 3);
  
        // apply equals()
        boolean bothAreEquals
            = weekFields.equals(otherWeekFields);
  
        // print results
        System.out.println("Equals: "
                           + bothAreEquals);
    }
}


Output:

Equals: false

References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/WeekFields.html#equals(java.lang.Object)

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS