Java 日历 clear(int cal_field)方法及实例
Calendar类中的 clear(int cal_field ) 方法用于设置给定的日历字段值和该日历的时间值。
注意:日历类的实现可以使用默认字段值进行日期和时间计算。
语法
public final void clear(int cal_field)
参数: 该方法需要一个整数类型的参数cal_field,它指的是要变成未定义的字段值。
返回值: 该方法不返回任何值。
下面的程序说明了日历类的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://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clear(int)