Java 日历 clear()方法及实例
Calendar类中的 clear() 方法用于设置所有的日历字段值和这个Calendar的时间值。
注意: 日历类的实现可以使用默认字段值来计算日期和时间。
语法
public final void clear()
参数: 该方法不接受任何参数。
返回值: 该方法不返回任何值。
下面的程序说明了日历类的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://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clear()