- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Date clone() Method
Description
The Java Date clone() returns a shallow copy of this Date object.
Declaration
Following is the declaration for java.util.Date.clone() method
public Object clone()
Parameters
NA
Return Value
This method does not return any value.
Exception
NA
Getting Clone of a Given Date Example
The following example shows the usage of Java Date clone() method. We're creating a Date instance and then its clone is created using clone() method. Both the dates are printed.
package com.tutorialspoint;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
// create a date
Date date = new Date(122, 10, 4);
// clone it to a second date
Object date2 = date.clone();
// print the results
System.out.println("Original Date:" + date.toString());
System.out.println("Cloned date :" + date2.toString());
}
}
Output
Let us compile and run the above program, this will produce the following result −
Original Date:Fri Nov 04 00:00:00 IST 2022 Cloned date :Fri Nov 04 00:00:00 IST 2022
Modifying Clone of a Given Date Example
The following example shows the modification done to cloned date instance created using Java Date clone() method. We're creating a Date instance and then its clone is created using clone() method. Both the dates are printed.
package com.tutorialspoint;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
// create a date
Date date = new Date(122, 10, 4);
// clone it to a second date
Object dateObj = date.clone();
// get the date instance
Date date2 = (Date) dateObj;
// update the month
date2.setMonth(8);
// print the results
System.out.println("Original Date:" + date.toString());
System.out.println("Cloned date :" + date2.toString());
}
}
Output
Let us compile and run the above program, this will produce the following result −
Original Date:Fri Nov 04 00:00:00 IST 2022 Cloned date :Sun Sep 04 00:00:00 IST 2022
Modifying Clone of a Given Date Example
The following is another example showing the modification done to cloned date instance created using Java Date clone() method. We're creating a Date instance and then its clone is created using clone() method. Both the dates are printed.
package com.tutorialspoint;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
// create a date
Date date = new Date(122, 10, 4);
// clone it to a second date
Date date2 = (Date) date.clone();
// update the month
date2.setMonth(8);
// print the results
System.out.println("Original Date:" + date.toString());
System.out.println("Cloned date :" + date2.toString());
}
}
Output
Let us compile and run the above program, this will produce the following result −
Original Date:Fri Nov 04 00:00:00 IST 2022 Cloned date :Sun Sep 04 00:00:00 IST 2022