📜  Java中的 Calendar clone() 方法及示例

📅  最后修改于: 2022-05-13 01:54:42.321000             🧑  作者: Mango

Java中的 Calendar clone() 方法及示例

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: Java/util/Calendar.html#clone()