Calendar类中的clear()方法用于设置所有日历字段值和此Calendar的时间值(未定义)。
注意:日历类的实现可以将默认字段值用于日期和时间计算。
用法:
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