首先我是这样写的代码:publicclasst2{publicstaticvoidmain(String[]args){Datedate=newDate();Dated1=date;System.out.println("date:"+date);d1.setTime(d1.getTime()-60*1000);Sy...
首先我是这样写的代码:
public class t2 {
public static void main(String[] args) {
Date date=new Date();
Date d1 = date;
System.out.println("date:"+date);
d1.setTime(d1.getTime()-60*1000);
System.out.println("d1:"+d1);
Date d2 = d1;
d2.setTime(d2.getTime()-60*1000);
System.out.println("d2:"+d2);
Date d3 = d2;
d3.setTime(d3.getTime()-60*1000);
System.out.println("d3:"+d3);
Date d4 = d3;
d4.setTime(d4.getTime()-60*1000);
System.out.println("d4:"+d4);
}
}
控制台速出结果为:
date:Wed Jul 22 16:20:33 CST 2015
d1:Wed Jul 22 16:19:33 CST 2015
d2:Wed Jul 22 16:18:33 CST 2015
d3:Wed Jul 22 16:17:33 CST 2015
d4:Wed Jul 22 16:16:33 CST 2015
但是如果我这样写:
public class t2 {
public static void main(String[] args) {
Date date=new Date();
Date d1 = date;
d1.setTime(d1.getTime()-60*1000);
Date d2 = d1;
d2.setTime(d2.getTime()-60*1000);
Date d3 = d2;
d3.setTime(d3.getTime()-60*1000);
Date d4 = d3;
d4.setTime(d4.getTime()-60*1000);
System.out.println("date:"+date);
System.out.println("d1:"+d1);
System.out.println("d2:"+d2);
System.out.println("d3:"+d3);
System.out.println("d4:"+d4);
}
}
控制台输出结果就变成:
date:Wed Jul 22 16:19:20 CST 2015
d1:Wed Jul 22 16:19:20 CST 2015
d2:Wed Jul 22 16:19:20 CST 2015
d3:Wed Jul 22 16:19:20 CST 2015
d4:Wed Jul 22 16:19:20 CST 2015
这是为什么呢?
我在执行上面一段代码时时间是16点20分,执行下面一段代码时时间是16点24分。
为什么下面代码的输出结果不是递减的,我要怎么定义d1、d2、d3、d4这些变量才能让下面的代码也能输出上面的递减效果呢
展开