有没有办法将Date对象复制到另一个日期对象而不使用引用? [英] Is there a way to copy Date object into another date Object without using a reference?

查看:1286
本文介绍了有没有办法将Date对象复制到另一个日期对象而不使用引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,用于加载原始数据以进行制图,并将其存储在名为 cRawGraph 的类中。然后,它将此数据格式化并将其存储在另一个名为<的类中code> cFormatGraph 。有没有办法将存储在 cRwGraph 中的一些日期对象复制到存储在 cFormattedGraph 中的日期对象,而不使用引用?我查看了Oracle的文档,但没有看到构造函数会接受日期对象或任何方法数据都可以完成此任务。

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.

代码段:

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());  


推荐答案

您可以使用getTime()并将其传递给日期(时间)构造函数。这只是必需的,因为Date是可变的。

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());

如果您使用的是Java 8,请尝试使用新的 java.time API ,它使用 不可变对象 。所以不需要复制/克隆。

If you're using Java 8 try using the new java.time API which uses immutable objects. So no need to copy/clone.

这篇关于有没有办法将Date对象复制到另一个日期对象而不使用引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆