20

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());  
Cartier
  • 429
  • 4
  • 15
Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • 1
    I am not sure what you are asking, but you can use `b = new Date(a.getTime())` or `b = (Date) a.clone();` to clone a date object. BTW: your sample code does not actually contain the word "Date". – eckes Feb 04 '15 at 22:20

4 Answers4

57

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Adam
  • 35,919
  • 9
  • 100
  • 137
  • Hi,Thank you for the answwer could you explain what immutable means? – Ted pottel Feb 08 '15 at 18:46
  • Immutable means "cannot be changed" ie the date is fixed, this means you can safely pass objects around without worrying about client code changing the value, and also removes the need to clone objects. – Adam Feb 08 '15 at 18:56
9

With Java 8 you can use the following nullsafe code.

Optional.ofNullable(oldDate)
               .map(Date::getTime)
               .map(Date::new)
               .orElse(null)
Nicolas Henneaux
  • 11,507
  • 11
  • 57
  • 82
3

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());
Alexis Murray
  • 678
  • 13
  • 23
  • 1
    Doesn't Java 8 come with its own version of this JodaTime class so you no longer need to use joda? – Daniel Kaplan Feb 04 '15 at 22:37
  • 2
    @DanielKaplan No, Joda-Time is alive and well. While Joda-Time did indeed inspire the new java.time package built into Java 8, **java.time was re-architected**. So java.time is not a drop-in replacement for Joda-Time. Both Joda-Time and java.time have strengths and features the other lacks. Both are ongoing active projects. They are not mutually exclusive; you can use both in a project (just be careful with imports as a few class names are coincidentally the same). The [threaten-extra project](http://www.threeten.org/threeten-extra/) extends java.time to add more features. – Basil Bourque Feb 05 '15 at 07:51
0

Date d = (Date) oldDate.clone()

will clone the current date, and then case the object to Date

PJ3
  • 3,870
  • 1
  • 30
  • 34