Calendar類中的clear()方法用於克隆日曆對象。它本質上會創建該對象的淺拷貝。
用法:
public Object clone()
參數:該方法不帶任何參數。
返回值:該方法不返回任何值。
以下示例程序旨在說明Calendar類的clone()方法的用法:
示例1:
// Java Code to illustrate clone() Method
import java.util.Calendar;
public class CalendarClassDemo {
public static void main(String args[])
{
// Creating a calendar object
Calendar calndr1 = Calendar.getInstance();
// Displaying current calendar
System.out.println("Original calendar: "
+ calndr1.getTime());
// Cloning the original
Calendar calndr2 = (Calendar)calndr1.clone();
// Displaying the Copy
System.out.println("Cloned calendar: "
+ calndr2.getTime());
}
}
輸出:
Original calendar: Tue Feb 12 11:41:36 UTC 2019 Cloned calendar: Tue Feb 12 11:41:36 UTC 2019
示例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());
// Cloning the original
Calendar calndr2 = (Calendar)calndr1.clone();
// Displaying the Copy
System.out.println("Cloned calendar: "
+ calndr2.getTime());
}
}
輸出:
Original calendar: Wed Jan 02 00:00:00 UTC 2019 Cloned calendar: Wed Jan 02 00:00:00 UTC 2019
參考: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clone()
相關用法
- Java Calendar set()用法及代碼示例
- Java Calendar get()用法及代碼示例
- Java Calendar add()用法及代碼示例
- Java ConcurrentSkipListMap clone()用法及代碼示例
- Java CharacterIterator clone()用法及代碼示例
- Java ArrayList clone()用法及代碼示例
- Java DateFormat clone()用法及代碼示例
- Java StringCharacterIterator clone()用法及代碼示例
- Java Properties clone()用法及代碼示例
- Java Date clone()用法及代碼示例
- Java Vector clone()用法及代碼示例
- Java SimpleDateFormat clone()用法及代碼示例
- Java TimeZone clone()用法及代碼示例
- Java NumberFormat clone()用法及代碼示例
- Java BitSet clone()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Calendar clone() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。