当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clone()



相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 Calendar clone() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。