Java中的Date类的clone()方法返回传递的Date对象的重复项。此重复项只是给定Date对象的浅拷贝。
用法:
public Object clone()
参数:该方法不接受任何参数。
返回值:该方法不返回任何值。
以下示例程序旨在说明clone()方法的使用:
示例1:
// Java code to demonstrate
// clone() method of Date class
import java.util.Date;
import java.util.Calendar;
public class GfG {
public static void main(String args[])
{
// Creating a Calendar object
Calendar c1 = Calendar.getInstance();
// Set Month
// MONTH starts with 0 i.e. ( 0 - Jan)
c1.set(Calendar.MONTH, 11);
// Set Date
c1.set(Calendar.DATE, 05);
// Set Year
c1.set(Calendar.YEAR, 1996);
// Creating a date object
// with specified time.
Date dateOne = c1.getTime();
Object dateTwo = dateOne.clone();
System.out.println("Original Date: "
+ dateOne.toString());
System.out.println("Cloned Date: "
+ dateTwo.toString());
}
}
输出:
Original Date: Thu Dec 05 05:39:04 UTC 1996 Cloned Date: Thu Dec 05 05:39:04 UTC 1996
示例2:
// Java code to demonstrate
// clone() method of Date class
import java.util.Date;
import java.util.Calendar;
public class GfG {
public static void main(String args[])
{
// Creating a Calendar object
Calendar c1 = Calendar.getInstance();
// Set Month
// MONTH starts with 0 i.e. ( 0 - Jan)
c1.set(Calendar.MONTH, 00);
// Set Date
c1.set(Calendar.DATE, 30);
// Set Year
c1.set(Calendar.YEAR, 2019);
// Creating a date object
// with specified time.
Date dateOne = c1.getTime();
Object dateTwo = dateOne.clone();
System.out.println("Original Date: "
+ dateOne.toString());
System.out.println("Cloned Date: "
+ dateTwo.toString());
}
}
输出:
Original Date: Wed Jan 30 05:39:10 UTC 2019 Cloned Date: Wed Jan 30 05:39:10 UTC 2019
参考: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#clone–
相关用法
- Java BitSet clone()用法及代码示例
- Java DateFormat clone()用法及代码示例
- Java Vector clone()用法及代码示例
- Java CharacterIterator clone()用法及代码示例
- Java StringCharacterIterator clone()用法及代码示例
- Java ConcurrentSkipListMap clone()用法及代码示例
- Java ArrayList clone()用法及代码示例
- Java Properties clone()用法及代码示例
- Java SimpleTimeZone clone()用法及代码示例
- Java Calendar clone()用法及代码示例
- Java Locale clone()用法及代码示例
- Java TimeZone clone()用法及代码示例
- Java SimpleDateFormat clone()用法及代码示例
- Java DecimalFormatSymbols clone()用法及代码示例
- Java NumberFormat clone()用法及代码示例
注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 Date clone() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。