📜  Java中的 Calendar clear() 方法及示例

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

Java中的 Calendar clear() 方法及示例

Calendar 类中的clear()方法用于设置该 Calendar 的所有日历字段值和时间值 undefined。
注意:日历类的实现可以使用默认字段值进行日期和时间计算。

句法:

public final void clear()

参数:该方法不带任何参数。

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

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

// 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 values
        // of the calendar
        calndr.clear();
  
        // Displaying the final result
        System.out.println("After clear method: "
                           + calndr.getTime());
    }
}
输出:
Current Date&Time: Tue Feb 12 13:01:42 UTC 2019
After clear method: Thu Jan 01 00:00:00 UTC 1970

示例 2:

// Java Code to illustrate clone() Method
  
import java.util.*;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr1 = new GregorianCalendar(2018, 12, 2);
  
        // Displaying current calendar
        System.out.println("Original calendar: "
                           + calndr1.getTime());
  
        // Clear method for undefining the values
        // of the calendar
        calndr1.clear();
  
        // Displaying the final result
        System.out.println("After clear method: "
                           + calndr1.getTime());
    }
}
输出:
Original calendar: Wed Jan 02 00:00:00 UTC 2019
After clear method: Thu Jan 01 00:00:00 UTC 1970

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