Java 8 Clock equals() 方法
可以使用 Java 中 Clock 类中的 equals() 方法检查两个 Java 时钟对象的相等性。此方法需要一个单一参数,即要与现有时钟对象进行比较的对象。此外,如果两个时钟对象相等,则返回 true,否则返回 false。
演示此内容的程序如下 -
示例
import java.time.Clock;
import java.time.ZoneId;
public class Demo {
public static void main(String[] args) {
Clock c1 = Clock.systemDefaultZone();
Clock c2 = Clock.systemDefaultZone();
System.out.println("Clock c1: " + c1.toString());
System.out.println("Clock c2: " + c2.toString());
boolean flag = c1.equals(c2);
if(flag)
System.out.println("
Both the clock objects are equal");
else
System.out.println("
Both the clock objects are not equal");
}
}输出
Clock c1: SystemClock[Etc/UTC] Clock c2: SystemClock[Etc/UTC] Both the clock objects are equal
现在让我们了解上述程序。
使用 equals() 方法比较时钟对象 c1 和 c2,并将返回的值存储在 flag 中。然后,它将显示对象是否相等。展示此内容的代码片段如下 -
Clock c1 = Clock.systemDefaultZone();
Clock c2 = Clock.systemDefaultZone();
System.out.println("Clock c1: " + c1.toString());
System.out.println("Clock c2: " + c2.toString());
boolean flag = c1.equals(c2);
if(flag)
System.out.println("
Both the clock objects are equal");
else
System.out.println("
Both the clock objects are not equal");
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP