Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to copy Date object into another date Object without using a reference?

Tags:

java

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());  
like image 425
Ted pottel Avatar asked Feb 04 '15 22:02

Ted pottel


People also ask

How do I make a copy of an existing object?

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.

Is Javascript date mutable?

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.


3 Answers

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.

like image 65
Adam Avatar answered Oct 13 '22 10:10

Adam


With Java 8 you can use the following nullsafe code.

Optional.ofNullable(oldDate)
               .map(Date::getTime)
               .map(Date::new)
               .orElse(null)
like image 36
Nicolas Henneaux Avatar answered Oct 13 '22 12:10

Nicolas Henneaux


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());
like image 26
Alexis Murray Avatar answered Oct 13 '22 11:10

Alexis Murray