📜  Java.util.Calendar.after() 方法

📅  最后修改于: 2022-05-13 01:54:57.030000             🧑  作者: Mango

Java.util.Calendar.after() 方法

Java.util.Calendar.after() 是Java.util 包的Calendar类中的一个方法。如果此 Calendar 表示的时间晚于when Object 表示的时间,则该方法返回true 。如果不是这种情况,则返回false

句法 :

public boolean after(Object when)

Where, when is the Object 
that is to be compared.

下面是一些示例,可以更好地理解 Calendar.after()函数的实现:

示例 1:

// Java code show the usage of
// after() 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();
                  
    // printing current date
    System.out.println("Time 1 : " + cal_obj1.getTime());
      
    // creating Calendar object     
    Calendar cal_obj2 = Calendar.getInstance();
      
    // printing current date
    System.out.println("Time 2 : " + cal_obj2.getTime());
                  
    // checking if 1st date is after 2nd date
    // and printing the result
    System.out.println(cal_obj1.after(cal_obj2));
    }
}

输出 :

Time 1 : Thu Mar 01 09:26:04 UTC 2018
Time 2 : Thu Mar 01 09:26:04 UTC 2018
false

示例 2:

// Java code to show the usage of
// after() 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);
  
        // check if calendar date is after current date
        System.out.println("Result : " + cal_obj1.after(cal_obj2));
    }
}

输出 :

Current date is : Thu Mar 01 09:27:19 UTC 2018
Result : true