java.util.GregorianCalendar.equals()方法是Java中的內置函數,該函數檢查此GregorianCalendar實例與作為參數傳遞給該函數的Object之間是否相等。僅當指定的Object是具有與此GregorianCalendar實例相同的時間值(距曆元毫秒的偏移量)的GregorianCalendar對象時,它才返回true。
用法:
public boolean equals(Object obj)
參數:該函數接受單個強製性參數obj,該參數將與此GregorianCalendar實例進行比較。
返回值:僅當指定的Object是GregorianCalendar對象並且具有與此實例相同的時間值(距紀元的毫秒偏移)時,此方法才返回true,否則返回false。
例子:
Input : c1 = Mon Jul 23 23:46:14 UTC 2018, c2 = Mon Jul 23 23:46:14 UTC 2018 Output : true Input : c1 = Mon Jul 23 23:46:14 UTC 2018, c2 = Sun Jul 24 00:02:52 UTC 2022 Output : false
下麵的程序演示了java.util.GregorianCalendar.equals()函數:
示例1:
// Java Program to illustrate the equals() function
// of GregorianCalendar class
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create a new calendar
GregorianCalendar c1 = (GregorianCalendar)
GregorianCalendar.getInstance();
// Display the current date and time
System.out.println("Current Date and Time : "
+ c1.getTime());
// Create a second calendar equal to first one
GregorianCalendar c2 =
(GregorianCalendar)(Calendar)c1.clone();
// Compare the two calendars
System.out.println("Both calendars are equal:"
+ c1.equals(c2));
// Adding 15 months to second calender
c2.add(GregorianCalendar.MONTH, 15);
// Display the current date and time
System.out.println("Modified Date and Time : "
+ c2.getTime());
// Compare the two calendars
System.out.println("Both calendars are equal:"
+ c1.equals(c2));
}
}
輸出:
Current Date and Time : Fri Jul 27 12:05:05 UTC 2018 Both calendars are equal:true Modified Date and Time : Sun Oct 27 12:05:05 UTC 2019 Both calendars are equal:false
示例2:
// Java Program to illustrate the equals() function
// of GregorianCalendar class
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create a new calendar
GregorianCalendar c1 = (GregorianCalendar)
GregorianCalendar.getInstance();
// Display the current date and time
System.out.println("Current Date and Time : "
+ c1.getTime());
// Create a second calendar equal to first one
GregorianCalendar c2 =
(GregorianCalendar)(Calendar)c1.clone();
// Compare the two calendars
System.out.println("Both calendars are equal:"
+ c1.equals(c2));
// Changing the Time Zone of c2
c2.setTimeZone(TimeZone.getTimeZone("CST"));
// Compare the two calendars
System.out.println("Both calendars are equal:"
+ c1.equals(c2));
}
}
輸出:
Current Date and Time : Fri Jul 27 12:05:08 UTC 2018 Both calendars are equal:true Both calendars are equal:false
參考: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#equals()
相關用法
- Java GregorianCalendar add()用法及代碼示例
- Java GregorianCalendar getMaximum()用法及代碼示例
- Java GregorianCalendar getMinimum()用法及代碼示例
- Java GregorianCalendar isLeapYear()用法及代碼示例
- Java GregorianCalendar hashCode()用法及代碼示例
- Java GregorianCalendar setTimeZone()用法及代碼示例
- Java GregorianCalendar getLeastMaximum()用法及代碼示例
- Java GregorianCalendar getGreatestMinimum()用法及代碼示例
- Java GregorianCalendar getTimeZone()用法及代碼示例
- Java GregorianCalendar clone()用法及代碼示例
- Java ParsePosition equals()用法及代碼示例
- Java DecimalStyle equals()用法及代碼示例
- Java CharBuffer equals()用法及代碼示例
- Java Set equals()用法及代碼示例
- Java BigInteger equals()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 GregorianCalendar equals() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。