當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java WeekFields equals()用法及代碼示例


WeekFields類的equals()方法用於比較此WeekFields是否等於作為參數傳遞的指定對象。比較是基於規則的整個狀態,即第一筆day-of-week和最少的天數。

用法:

public boolean equals(Object object)

參數:此方法接受與其他規則進行比較的對象,null返回false。


返回值:如果等於指定的規則,則此方法返回true。

以下示例程序旨在說明WeekFields.equals()方法:
程序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); 
    } 
}
輸出:
Equals:true

程序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); 
    } 
}
輸出:
Equals:false

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



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 WeekFields equals() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。