java.util.Calendar clone()方法
java.util.Calendar.clone() 方法是对象类的方法,而Object类是所有Java类的超类。也可以为日历类的对象调用clone() 方法。克隆方法返回对象的副本。
1 语法
public Object clone()
2 参数
无
3 返回值
此方法返回此对象的副本。
4 示例1
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar clone()方法的例子
*/
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
5 示例2
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar clone()方法的例子
*/
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
6 示例3
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar clone()方法的例子
*/
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
7 示例4
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.Calendar clone()方法的例子
*/
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
热门文章
优秀文章