当前位置: 技术问答>java相关
这段JAVA程序哪里出错阿?
来源: 互联网 发布时间:2015-03-30
本文导语: import java.sql.Timestamp; class test{ public static void main(String[] args){ Timestamp beginDate = new Timestamp(2002-1900,1,8,0,0,0,0); Timestamp endDate = new Timestamp(2002-1900,2,17,0,0,0,0); String unit = "day"; int periodId = 0; int num =...
import java.sql.Timestamp;
class test{
public static void main(String[] args){
Timestamp beginDate = new Timestamp(2002-1900,1,8,0,0,0,0);
Timestamp endDate = new Timestamp(2002-1900,2,17,0,0,0,0);
String unit = "day";
int periodId = 0;
int num = 33;
Timestamp tempDate = beginDate;
int tempMonth,tempDay;
if(unit.equals("day")){
tempDay = num;
while(endDate.after(tempDate)){
System.out.println("------------------------------------------------");
tempDate.setDate(tempDate.getDate()+tempDay);
++periodId;
System.out.println("insert value is "+new Integer(periodId).intValue()+" " + beginDate.toString()+ " "
+tempDate.toString());
beginDate = tempDate;
}
}
}
}
本来想达到的目的是根据起始日期和截至日期以及间隔天数向数据库插入数据
如果起始和截至日期分别是2002.1.8和2002.3.17
间隔天数是33天
那插入的2个记录应该是
1:2002-01-08,2002-03-13
2:2002-03-13-2002-03-17
class test{
public static void main(String[] args){
Timestamp beginDate = new Timestamp(2002-1900,1,8,0,0,0,0);
Timestamp endDate = new Timestamp(2002-1900,2,17,0,0,0,0);
String unit = "day";
int periodId = 0;
int num = 33;
Timestamp tempDate = beginDate;
int tempMonth,tempDay;
if(unit.equals("day")){
tempDay = num;
while(endDate.after(tempDate)){
System.out.println("------------------------------------------------");
tempDate.setDate(tempDate.getDate()+tempDay);
++periodId;
System.out.println("insert value is "+new Integer(periodId).intValue()+" " + beginDate.toString()+ " "
+tempDate.toString());
beginDate = tempDate;
}
}
}
}
本来想达到的目的是根据起始日期和截至日期以及间隔天数向数据库插入数据
如果起始和截至日期分别是2002.1.8和2002.3.17
间隔天数是33天
那插入的2个记录应该是
1:2002-01-08,2002-03-13
2:2002-03-13-2002-03-17
|
原因很简单
Timestamp tempDate = beginDate;
tempDate 和 beginDate 引用了同一个对象
对其中一个值的操作就会对另一个产生影响的
Timestamp tempDate = beginDate;
tempDate 和 beginDate 引用了同一个对象
对其中一个值的操作就会对另一个产生影响的