当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DateFormat equals()用法及代码示例


DateFormat类的equals()方法用于比较两个DateFormat对象。如果此DateFormat等于传递的对象,则该方法返回True,否则返回False。

用法:

public boolean equals(Object obj)

参数:该方法采用Object类型的一个参数obj,表示要与此DateFormat对象进行比较的对象。


返回值:如果两个对象相等,则该方法返回布尔True,否则返回False。

以下示例程序旨在说明DateFormat的equals()方法的用法:
示例1:

// Java code to illustrate equals() method 
  
import java.text.*; 
import java.util.*; 
  
public class DateFormat_Demo { 
    public static void main(String[] args) 
    { 
        // Initializing the first formatter 
        DateFormat DFormat1 
            = DateFormat.getDateInstance(); 
  
        // Initializing the second formatter 
        DateFormat DFormat2 
            = DateFormat.getDateInstance(); 
  
        // Displaying both the Formats 
        Date date = new Date(); 
        String str_Date1 
            = DFormat1.format(date); 
        System.out.println("First: "
                           + (str_Date1)); 
  
        String str_Date2 
            = DFormat2.format(date); 
        System.out.println("Second: "
                           + (str_Date2)); 
  
        // Comparing both the objects 
        System.out.println("Equality: "
                           + DFormat1.equals(DFormat2)); 
    } 
}
输出:
First: Mar 27, 2019
Second: Mar 27, 2019
Equality: true

示例2:

// Java code to illustrate equals() method 
  
import java.text.*; 
import java.util.*; 
  
public class DateFormat_Demo { 
    public static void main(String[] args) 
    { 
        // Initializing the first formatter 
        DateFormat DFormat1 
            = DateFormat.getDateInstance(); 
  
        // Initializing the second formatter 
        DateFormat DFormat2 
            = DateFormat.getTimeInstance(); 
  
        // Displaying both the Formats 
        Date date = new Date(); 
        String str_Date1 
            = DFormat1.format(date); 
        System.out.println("First: "
                           + (str_Date1)); 
  
        String str_Date2 
            = DFormat2.format(date); 
        System.out.println("Second: "
                           + (str_Date2)); 
  
        // Comparing both the objects 
        System.out.println("Equality: "
                           + DFormat1.equals(DFormat2)); 
    } 
}
输出:
First: Mar 27, 2019
Second: 11:04:25 AM
Equality: false

参考: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#equals(java.lang.Object)



相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 DateFormat equals() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。