equals() 方法比较两个对象是否相等,如果相等则返回 true。 equals() 方法在 Object 类中提供。因为对象类是java中所有其他类的超类。 Calendar 类继承了 equals() 方法。它将此(当前日历对象)日历与指定的对象进行比较。当且仅当参数是同一日历系统的 Calendar 对象,该对象在与此对象相同的 Calendar 参数下表示相同的时间值时,结果才为真。
用法
public boolean equals(Object object)
参数
对象:要比较的对象
返回
它返回一个布尔值。
抛出
不要抛出任何异常。
例子1
import java.util.Calendar;
public class CalendarEqualsExample1 {
public static void main(String[] args) {
// create a new calendar object
Calendar cal1 = (Calendar) Calendar.getInstance();
// print the current date and time
System.out.println("" + cal1.getTime());
// create a second calendar object
Calendar cal2 = (Calendar) Calendar.getInstance();
// print cal2
System.out.println("" + cal2.getTime());
// compare the first and second calendar object
System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
}
}
输出:
Wed Aug 01 02:31:56 PDT 2018 Wed Aug 01 02:31:57 PDT 2018 Cal1 and Cal2 are equal:false
例子2
import java.util.Calendar;
public class CalendarEqualsExample2 {
public static void main(String[] args) {
// create a new calendar object
Calendar cal1 = (Calendar) Calendar.getInstance();
// print the current date and time
System.out.println("" + cal1.getTime());
// create clone of first calendar object
Calendar cal2 = (Calendar) (Calendar) cal1.clone();
// print cal2
System.out.println("" + cal2.getTime());
// compare the first calendar object and cloned calendar object
System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
}
}
输出:
Wed Aug 01 02:34:20 PDT 2018 Wed Aug 01 02:34:20 PDT 2018 Cal1 and Cal2 are equal:true
例子3
import java.util.Calendar;
public class CalendarEqualsExample3 {
public static void main(String[] args) {
// create a new calendar object
Calendar cal1 = (Calendar) Calendar.getInstance();
// print the current date and time
System.out.println("" + cal1.getTime());
// create a clone of first calendar object
Calendar cal2 = (Calendar) (Calendar) cal1.clone();
// print cal2
System.out.println("" + cal2.getTime());
// change YEAR field of cloned calendar object
cal2.add(Calendar.YEAR, 5);
// compare the two calendars
System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
}
}
输出:
Wed Aug 01 02:36:22 PDT 2018 Wed Aug 01 02:36:22 PDT 2018 Cal1 and Cal2 are equal:false
相关用法
- Java Calendar equals()用法及代码示例
- Java Calendar get()用法及代码示例
- Java Calendar add()用法及代码示例
- Java Calendar getWeekYear()用法及代码示例
- Java Calendar computeFields()用法及代码示例
- Java Calendar after()用法及代码示例
- Java Calendar setTimeZone()用法及代码示例
- Java Calendar setFirstDayOfWeek()用法及代码示例
- Java Calendar setTimeInMillis()用法及代码示例
- Java Calendar complete()用法及代码示例
- Java Calendar compareTo()用法及代码示例
- Java Calendar getDisplayName()用法及代码示例
- Java Calendar setWeekDate()用法及代码示例
- Java Calendar getGreatestMinimum()用法及代码示例
- Java Calendar clear()用法及代码示例
- Java Calendar roll(int calndr_field, int amt)用法及代码示例
- Java Calendar setMinimalDaysInFirstWeek()用法及代码示例
- Java Calendar clear(int cal_field)用法及代码示例
- Java Calendar ComputeFields()用法及代码示例
- Java Calendar getCalendarType()用法及代码示例
注:本文由纯净天空筛选整理自 Java Calendar equals() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。