当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Calendar clear(int cal_field)用法及代码示例


Calendar类中的clear(int cal_field)方法用于设置给定的日历字段值和此Calendar的时间值(未定义)。注意:日历类的实现可能使用默认字段值进行日期和时间计算。

用法:

public final void clear(int cal_field)

参数:该方法采用整数类型的一个参数cal_field,表示要使其不确定的字段值。


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

以下示例程序旨在说明Calendar类的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)



相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 Calendar clear(int cal_field) Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。