當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。