Question:
In Java, how do I make a deep copy of a Date?
pnelnik
2010-03-30 01:31:55 UTC
When I assign a Date in java, I'm just adding a reference to the original date.
But how can I make a deep copy?

In the example below, what I would like to happen is:
- I assign d2 to d1,
- then update d1, but find that d2 is unaffected.
But what I find is that d2 is affected,
since the assignment was just adding a reference (shallow copy)
rather than a deep copy which is what I want.

/////////////////////////////////////////////////////////////////////////////
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;

class Test4
{
public static void main(String[] args)
{
try
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = formatter.parse("1980-10-10");

Date d2;
d2 = d1; // adds a reference to d1

d1.setMonth(4); // set d1 to May ( I know setMonth(..) is deprecated but ...)
System.out.println("d2 " + d2); // we find d2 is now also May
}
catch (Exception e)
{ // Catch exception if any
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
} // end of method: main(..)
} // end of class Test4

/////////////////////////////////////////////////////////////////////////////
I tried the following:
d2 = new Date(d1); // no such constructor
and d2 = d1.clone(); // incompatible types, found: java.lang.Object, required: java.util.Date
But neither compiled.
Five answers:
BHC
2010-04-04 18:02:49 UTC
This will work:



d2 = (Date)d1.clone();



So will this.



d2 = new Date(d1.getTime())



The first approach uses the clone method, but adds one important step you overlooked. Since the clone method returns an Object, you have to cast it to a Date before you can store it in a Date reference. The second method, which I prefer, converts the date to its Unix timestamp and then back into a date.



In Response to JavaIntermediate's question:



Take a look at the definition of the Calendar class. A "calendar" is nothing more than a set of rules about how dates are represented. For example, there are 60 seconds in a minute, 24 hours in a day, etc. A customized calendar could define 1000 seconds as a "kilosecond" and define our entire concept of time in terms of powers of 10. The Gregorian calendar is what we are all used to.



So a "Date" is an instant in time. A Calendar is a set of rules for representing time.



The fact that BaseCalendar is static does not preclude you from having more than one instance of Date. In fact, it doesn't stop you from using more than one Calendar either, but that is a topic for another discussion.
2016-12-16 14:44:17 UTC
Deep Copy Java
boyes
2016-10-07 04:31:06 UTC
Java Deep Copy
JavaIntermediate
2010-03-30 08:26:18 UTC
If you look at the Date API it uses Calendar and it sets it too static the actual date is stored in



private transient BaseCalendar.Date cdate;



but BaseCalendar is static



private static BaseCalendar jcal;



Meaning that every instance of Date that is created is going to be the same date. I run into the same problem since date has been depreciated to Calendar since 1.1. The only thing I've done to get around this is just to store the information I need in a String then set the date to the String and then print or manipulate then restore back into the String. If there is a better way I'm hoping my answer will start a discussion and get us both a better way if not then this might be the only way.... Or who knows I could be way wrong!!
2016-03-03 07:31:12 UTC
1. You can link back to your previous question by posting the link. 2. Have you tried running msconfig and removing everything from start up. Anti-virus software and spyware software can remove a virus, but if it doesn't remove the "start up files" it can re-infest your system the next time you boot up. Start | Run | type "msconfig" (without ")| Choose Selective Startup | Click on the Startup Tab | and select Deselect ALL. Reboot and run anti-virus and spyware software again. Then you can try adding back in the ones that are attached to your anti-virus software (example: C:\prog~1\mcafee\...). That way your virus software will be back to running in the background. Also be aware that the virus can be removed but not always the damage it has done.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...