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


Java Calendar clear()用法及代碼示例


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()



相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Calendar clear() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。