关于equals的Java代码_Java Calendar.equals()用法及代码示例

java.util.Calendar.equals()是java.util包Calendar类中的方法。该方法将此Calendar与指定的Object进行比较。如果此对象与object相等,则该方法返回true。如果不是这种情况,即两个日历之间的参数存在差异,则返回false。

用法:

public boolean equals(Object object)

Where, object is the Object

to be compared with.

下面是一些示例,以更好的方式理解Calendar.equals()函数的实现。

示例1:

Java

// Java code to show the use of

// equals() method of Calendar class

import java.util.*;

class GFG {

// Driver code

public static void main(String[] args)

throws InterruptedException {

// creating calendar object

Calendar cal_obj1 = Calendar.getInstance();

Calendar cal_obj2 = cal_obj1;

// printing current date

System.out.println("Time 1 : " + cal_obj1.getTime());

// printing current date

System.out.println("Time 2 : " + cal_obj2.getTime());

// checking if 1st date is equal to 2nd date

// and printing the result

System.out.println(cal_obj1.equals(cal_obj2));

}

}

输出:Time 1 : Thu Mar 01 09:36:17 UTC 2018

Time 2 : Thu Mar 01 09:36:17 UTC 2018

true

示例2:

Java

// Java code to show the use of

// equals() method of Calendar class

import java.util.*;

class GFG {

// Driver code

public static void main(String[] args) {

// creating calendar objects

Calendar cal_obj1 = Calendar.getInstance();

Calendar cal_obj2 = Calendar.getInstance();

// displaying the current date

System.out.println("Current date is : " +

cal_obj1.getTime());

// changing year in cal_obj2 calendar

cal_obj2.set(Calendar.YEAR, 2010);

// displaying the year

System.out.println("Year is " +

cal_obj2.get(Calendar.YEAR));

// check if calendar date is equal to current date

System.out.println("Result : " +

cal_obj1.equals(cal_obj2));

}

}

输出:Current date is : Thu Mar 01 09:39:30 UTC 2018

Year is 2010

Result : false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值