📜  带有示例的Java中的 Calendar clear(int cal_field) 方法

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

带有示例的Java中的 Calendar clear(int cal_field) 方法

Calendar 类中的clear(int cal_field )方法用于设置给定的日历字段值,并且此 Calendar 的时间值未定义。
注意:日历类的实现可以使用默认字段值进行日期和时间计算。

句法:

public final void clear(int cal_field)

参数:该方法采用一个整数类型的参数cal_field ,并引用要使其未定义的字段值。

返回值:该方法不返回任何值。

下面的程序说明了 Calendar 类的 clear() 方法的工作:
示例 1:

// Java Code to illustrate clear() Method
  
import java.util.Calendar;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the Current Date
        System.out.println("Current Date&Time: "
                           + calndr.getTime());
  
        // Clear method for undefining the month
        // of the calendar
        calndr.clear(Calendar.MONTH);
  
        // Displaying the final result
        System.out.println("After clear method: "
                           + calndr.getTime());
    }
}
输出:
Current Date&Time: Wed Mar 27 09:03:26 UTC 2019
After clear method: Sun Jan 27 09:03:26 UTC 2019

示例 2:

// Java Code to illustrate clear() Method
  
import java.util.Calendar;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the Current Date
        System.out.println("Current Date&Time: "
                           + calndr.getTime());
  
        // Clear method for undefining the year
        // of the calendar
        calndr.clear(Calendar.YEAR);
  
        // Displaying the final result
        System.out.println("After clear method: "
                           + calndr.getTime());
    }
}
输出:
Current Date&Time: Wed Mar 27 09:03:32 UTC 2019
After clear method: Fri Mar 27 09:03:32 UTC 1970

参考: https: Java/util/Calendar.html#clear(int)