GregorianCalendar類的java.util.GregorianCalendar.clone()方法用於創建一個新對象,並將該GregorianCalendar實例的所有內容複製到新對象中。
用法:
public Object clone()
參數:該函數不接受任何參數。
返回值:此函數返回此對象的副本。
例子:
Input: Mon Jul 23 14:35:27 UTC 2018 Output: Mon Jul 23 14:35:27 UTC 2018 Input: Current Date and Time is Mon Jul 23 14:35:27 UTC 2018 cal1.add((GregorianCalendar.MONTH), -7); cal1.clone(); Output: Sat Dec 23 14:36:42 UTC 2017
以下示例程序旨在說明java.util.GregorianCalendar.clone()方法:
示例1:
// Java Program to illustrate GregorianCalendar.clone()
// function
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Creating a new calendar
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
// Display the date and time
System.out.println("Date and Time in"
+" cal object : "+ cal.getTime());
GregorianCalendar newcalender =
new GregorianCalendar();
// Cloning the object
newcalender = (GregorianCalendar)cal.clone();
// Display date and time
System.out.println("Date and Time in"+
" newcalender object : "+ newcalender.getTime());
}
}
輸出:
Date and Time in cal object : Fri Aug 03 11:01:24 UTC 2018 Date and Time in newcalender object : Fri Aug 03 11:01:24 UTC 2018
示例2:
// Java Program to illustrate
// GregorianCalendar.clone()
// function
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Creating a new calendar
GregorianCalendar cal1, cal2;
cal1 = (GregorianCalendar)GregorianCalendar.
getInstance();
// Display the current date and time
System.out.println("Current Date and Time : "
+ cal1.getTime());
// Modifying the current date and time
cal1.add((GregorianCalendar.MONTH), 2);
// Cloning the object
cal2 = (GregorianCalendar)cal1.clone();
// Display date and time
System.out.println("New Date and Time : "
+ cal2.getTime());
}
}
輸出:
Current Date and Time : Fri Aug 03 11:01:27 UTC 2018 New Date and Time : Wed Oct 03 11:01:27 UTC 2018
參考: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#clone()
相關用法
- Java GregorianCalendar add()用法及代碼示例
- Java GregorianCalendar hashCode()用法及代碼示例
- Java GregorianCalendar equals()用法及代碼示例
- Java GregorianCalendar getMinimum()用法及代碼示例
- Java GregorianCalendar getMaximum()用法及代碼示例
- Java GregorianCalendar isLeapYear()用法及代碼示例
- Java GregorianCalendar getGreatestMinimum()用法及代碼示例
- Java GregorianCalendar getLeastMaximum()用法及代碼示例
- Java GregorianCalendar setTimeZone()用法及代碼示例
- Java GregorianCalendar getTimeZone()用法及代碼示例
- Java IdentityHashMap clone()用法及代碼示例
- Java Hashtable clone()用法及代碼示例
- Java RuleBasedCollator clone()用法及代碼示例
- Java HashMap clone()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 GregorianCalendar clone() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。