我正在尝试以下代码以打开某个目录中的文件。文件的名称是按日期分配的,但缺少一些日期。我想遍历日期以获取文件,并使代码每次无法找到文件时都返回1天,直到它找到一个文件(currentdate
是一个全局变量,而奇怪的xml元素是因为我正在使用处理)。重试打开目录中的文件
我觉得代码应该做的是:
- 尝试打开该文件用给出的日期。
- 错误,它会抓住并获得一个新的日期。
- 重复此过程直至找到有效日期。
- 当找到有效日期时,它会转到
break
所在的行,并退出循环。
但由于某些原因,它像编辑#怪异的东西,有时它跳跃得太多,尤其是临近正月# 是我的逻辑不工作的一些原因? 感谢
String strdate=getdatestring(counter);
int counter=0;
while(true){
try{
xmldata = new XMLElement(this, "dir/" + strdate + "_filename.xml");
break;
}catch(NullPointerException e){
counter +=1;
strdate=getdatestring(counter);
}}
String getdatestring(int counter) {
Date firstdate=new Date();
int daystosum=0;
String strcurrentdate="";
if(keyPressed && key=='7'){
daystosum=-7;
}
daystosum=daystosum-counter;
Calendar c=Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try{
firstdate=formatter.parse("2012-04-13");//first day of the database
}catch(ParseException e){
println(e);
}
c.setTime(currentdate);
c.add(Calendar.DATE,daystosum);
currentdate=c.getTime();
if(currentdate.before(firstdate)){
currentdate=firstdate;
}
strcurrentdate=formatter.format(currentdate);
return strcurrentdate;
}
你的意思是当'firstdate'在这一行'firstdate = formatter.parse(“2012-04-13”);'是将来吗? –
对不起,这个问题其实不是真的..我刚刚编辑对不起了。这很奇怪,因为在4月份,这是第一个月的文件,当我从5月3日移动到例如1周到4月28日,那么它一直到数据库的第一天,即4月13日,但它不在其他几个月的行为不检,至少我没有注意到..即时通讯检查更多的错误,但需要一段时间来加载 – fartagaintuxedo
我只是想了解,哪些数据将来会导致您的问题?在'getdatestring'中只有外部值是firstDate到上面的行。我是否错过了您的问题域? –