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


Java Calendar Clone()用法及代码示例


clone()方法是对象类的方法,对象类是java所有类的超类。日历类的对象也可以调用 clone() 方法。 Clone 方法返回对象的副本。

用法

public Object clone()

参数

此方法不接受任何值。

返回

此方法返回此对象的副本。

抛出

CloneNotSupportedException。

例子1

import java.util.Calendar;
public class CalendarCloneExample1 {
public static void main(String[] args) {
      // create new calendar object.
     Calendar cal = (Calendar)  Calendar.getInstance();
      // print date for default value
      System.out.println("Actual Calendar Object:" + cal.getTime());
      // create a clone of first calendar object
     Calendar cal2 = (Calendar) cal.clone();
      // display the copy
      System.out.println("Cloned calendar object:" + cal2.getTime());
   }
}

输出:

Actual Calendar Object:Mon Jul 30 09:28:59 PDT 2018
Cloned calendar object:Mon Jul 30 09:28:59 PDT 2018

例子2

import java.util.Calendar;
public class CalendarCloneExample2 {
public static void main(String[] args) {
      // create a new calendar object
     Calendar cal = (Calendar) Calendar.getInstance();
      // print the current date and time
      System.out.println("" + cal.getTime());
// add 9 years to the YEAR feild
      cal.add((Calendar.YEAR), 9);
// create clone of first calendar object      
Calendar cal2 = (Calendar) cal.clone();
      // display the copy
      System.out.println("Cloned calendar object:" + cal2.getTime());
}
}

输出:

Mon Jul 30 09:39:08 PDT 2018
Cloned calendar object:Fri Jul 30 09:39:08 PDT 2027

例子3

import java.util.Calendar;
public class CalendarCloneExample3 {
    public static void main(String[] args) {
    // create a new calendar
       Calendar cal = (Calendar) Calendar.getInstance();
      // print the current date and time
      System.out.println("" + cal.getTime());
// add 4 months to the MONTH feild
      cal.add((Calendar.MONTH), 4);
      //clone the first calendar object
Calendar cal2 = (Calendar) cal.clone();
      // display the copy
      System.out.println("Cloned calendar object:" + cal2.getTime());      
}
}

输出:

Mon Jul 30 09:42:52 PDT 2018
Cloned calendar object:Fri Nov 30 09:42:52 PST 2018

示例 4

import java.util.Calendar;
public class CalendarCloneExample4 {
public static void main(String[] args) {
      // create a new calendar
     Calendar cal = (Calendar) Calendar.getInstance();
      // print the current date and time
      System.out.println("" + cal.getTime());
      cal.add((Calendar.MONTH), 2);
      cal.add((Calendar.YEAR), 4);
      cal.add((Calendar.DAY_OF_MONTH), 24);
      cal.add((Calendar.HOUR), 5);
      cal.add((Calendar.MINUTE), 60);
      cal.add((Calendar.SECOND), 360);
Calendar cal2 = (Calendar) cal.clone();
      // display the copy
      System.out.println("Cloned calendar object:" + cal2.getTime());
}
}

输出:

Mon Jul 30 09:47:25 PDT 2018
Cloned calendar object:Mon Oct 24 15:53:25 PDT 2022



相关用法


注:本文由纯净天空筛选整理自 Java Calendar Clone() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。