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
参考: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clear()
相关用法
- Java Calendar clear(int cal_field)用法及代码示例
- Java Calendar add()用法及代码示例
- Java Calendar get()用法及代码示例
- Java Calendar set()用法及代码示例
- Java Calendar setFirstDayOfWeek()用法及代码示例
- Java Calendar hashCode()用法及代码示例
- Java Calendar getTimeZone()用法及代码示例
- Java Calendar toString()用法及代码示例
- Java Calendar internalGet()用法及代码示例
- Java Calendar getInstance()用法及代码示例
- Java Calendar getLeastMaximum()用法及代码示例
- Java Calendar getMinimum()用法及代码示例
- Java Calendar getMaximum()用法及代码示例
- Java Calendar clone()用法及代码示例
- Java Calendar getTimeInMillis()用法及代码示例
注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 Calendar clear() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。