I got a program that loads in raw data for charting and stores it in a class called cRawGraph
.. It then formats this data and stores it in another class called cFormatGraph
. Is there a way to copy some of the date objects stored in cRwGraph
to date objects stored in cFormattedGraph
without using a reference? I looked at Oracle's documentation and did not see a constructor that would take in a date object or any methods data would accomplish this.
code snippet:
do{
d=rawData.mDate[i].getDay();
da=rawData.mDate[i];
datept=i;
do{
vol+=rawData.mVol[i];
pt+=rawData.mPt[i];
c++;
i++;
if (i>=rawData.getSize())
break;
} while(d==rawData.mDate[i].getDay());
// this IS NOT WORKING BECOUSE IT IS A REFRENCE AND RawData gets loaded with new dates,
// Thus chnaging the value in mDate
mDate[ii]=da;
mVol[ii]=vol;
mPt[ii]=pt/c;
if (first)
{
smallest=biggest=pt/c;
first=false;
}
else
{
double temp=pt/c;
if (temp<smallest)
smallest=temp;
if (temp>biggest)
biggest=temp;
}
ii++;
} while(i<rawData.getSize());
To create a copy of an existing object in the vault, right-click the object of your choice and select Make Copy from the context menu. This command creates an entirely new object using the metadata and contents of the source object.
It seems that much like in Java, dates in Javascript are mutable, meaning that it is possible to change a date after it has been created. We had this painfully shown to us when using the datejs library to manipulate some dates.
You could use getTime() and passing it into the Date(time) constructor. This is only required because Date is mutable.
Date original = new Date();
Date copy = new Date(original.getTime());
If you're using Java 8 try using the new java.time API which uses immutable objects. So no need to copy/clone.
With Java 8 you can use the following nullsafe code.
Optional.ofNullable(oldDate)
.map(Date::getTime)
.map(Date::new)
.orElse(null)
If possible, try switching to using Joda Time instead of the built in Date type.
http://www.joda.org/joda-time/
DateTime from Joda has a copy constructor, and is generally nicer to work with since DateTime is not mutable.
Otherwise you could try:
Date newDate = new Date(oldDate.getTime());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With