我在Java.sql.Timestamp对象中有一个日期(变量-> confirmedDeliveryDate),我想创建一个不同的对象,并在其中添加几天时间。我将ts创建为一个不同的时间戳变量,并使用了以下逻辑。我能成功地增加天数。但是,一旦ts被更新到新的时间,confirmedDeliveryDate也在用新的日期进行更新。当我为ts创建了一个新对象时,为什么要更新confirmedDeliveryDate。请提出同样的解决方案。
Timestamp ts = new Timestamp(0L);
ts=confirmedDeliveryDate;
Calendar cal = Calendar.getInstance();
cal.setTime(ts);
cal.add(Calendar.DAY_OF_WEEK, 5);
ts.setTime(cal.getTime().getTime());
ts = new Timestamp(cal.getTime().getTime());发布于 2015-12-30 13:05:01
通过编码
ts=confirmedDeliveryDate;您丢弃新创建的时间戳,并将其替换为对confirmedDeliveryDate的引用。
因此,现在ts和confirmedDeliveryDate是one和相同的,通过对其中一个变量调用setTime,两个变量名(引用相同对象)的值将发生变化。
就像这样:
Calendar cal = Calendar.getInstance();
cal.setTime(confirmedDeliveryDate );
cal.add(Calendar.DAY_OF_WEEK, 5);
Timestamp ts = new Timestamp(cal.getTime().getTime());https://stackoverflow.com/questions/34529602
复制相似问题