我有一个查询日期列表(每月的几天)和一个随机日期数的日期列表(CoC)。 我正在开发一个java应用程序来扫描月中的日期,并在每月的所有日期和其他列表的相对日期进行更改时写入输出。 我知道,我是不洁净的,但我已经写了一个简单的代码来向你说清楚。
这是我的(错误的)代码:
import java.util.ArrayList;
import org.joda.time.MutableDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Test
{
public static void main(String[] args)
{
int count_coc = 0;
String[] month = {"2014-04-28", "2014-04-29", "2014-04-30", "2014-05-01",
"2014-05-02", "2014-05-03", "2014-05-04", "2014-05-05", "2014-05-06",
"2014-05-07", "2014-05-08", "2014-05-09", "2014-05-10", "2014-05-11",
"2014-05-12", "2014-05-13", "2014-05-14", "2014-05-15", "2014-05-16",
"2014-05-17", "2014-05-18", "2014-05-19", "2014-05-20", "2014-05-21",
"2014-05-22", "2014-05-23", "2014-05-24", "2014-05-25", "2014-05-26",
"2014-05-27", "2014-05-28", "2014-05-29", "2014-05-30", "2014-05-31", "2014-06-01"};
String[] CoC = {"2014-04-30", "2014-05-15", "2014-05-24"};
DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd");
MutableDateTime dateElab = new MutableDateTime(dtf.parseDateTime(month[0]));
MutableDateTime dateLast = new MutableDateTime(dtf.parseDateTime(month[month.length-1]));
ArrayList<MutableDateTime> dtJs = new ArrayList<MutableDateTime>();
for (int i = 0; i<CoC.length;i++)
{
dtJs.add(new MutableDateTime(dtf.parseDateTime(CoC[i])));
}
do
{
if (dtJs.size() > 1)
{
if ((count_coc) < (dtJs.size()-1))
{
if (dateElab.isBefore(dtJs.get(count_coc+1)) || dateElab.isEqual(dtJs.get(count_coc+1)))
{
System.out.println("Date month: " + dtf.print(dateElab) + " Date CoC: " + dtf.print(dtJs.get(count_coc)));
if ((count_coc) > dtJs.size())
{
System.out.println("Date month: " + dtf.print(dateElab) + " Date CoC: " + dtf.print(dtJs.get(count_coc)));
}
else
{
count_coc++;
}
}
}
else
{
System.out.println("Date month: " + dtf.print(dateElab) + " Date CoC: " + dtf.print(dtJs.get(count_coc)));
}
}
else
{
System.out.println("Date month: " + dtf.print(dateElab) + " Date CoC: " + dtf.print(dtJs.get(count_coc)));
}
dateElab.addDays(1);
}
while(dateElab.isBefore(dateLast) || dateElab.isEqual(dateLast));
}
}
结果如下:
Date month: 2014-04-28 Date CoC: 2014-04-30
Date month: 2014-04-29 Date CoC: 2014-05-15
Date month: 2014-04-30 Date CoC: 2014-05-24
Date month: 2014-05-01 Date CoC: 2014-05-24
Date month: 2014-05-02 Date CoC: 2014-05-24
Date month: 2014-05-03 Date CoC: 2014-05-24
Date month: 2014-05-04 Date CoC: 2014-05-24
Date month: 2014-05-05 Date CoC: 2014-05-24
Date month: 2014-05-06 Date CoC: 2014-05-24
Date month: 2014-05-07 Date CoC: 2014-05-24
Date month: 2014-05-08 Date CoC: 2014-05-24
Date month: 2014-05-09 Date CoC: 2014-05-24
Date month: 2014-05-10 Date CoC: 2014-05-24
Date month: 2014-05-11 Date CoC: 2014-05-24
Date month: 2014-05-12 Date CoC: 2014-05-24
Date month: 2014-05-13 Date CoC: 2014-05-24
Date month: 2014-05-14 Date CoC: 2014-05-24
Date month: 2014-05-15 Date CoC: 2014-05-24
Date month: 2014-05-16 Date CoC: 2014-05-24
Date month: 2014-05-17 Date CoC: 2014-05-24
Date month: 2014-05-18 Date CoC: 2014-05-24
Date month: 2014-05-19 Date CoC: 2014-05-24
Date month: 2014-05-20 Date CoC: 2014-05-24
Date month: 2014-05-21 Date CoC: 2014-05-24
Date month: 2014-05-22 Date CoC: 2014-05-24
Date month: 2014-05-23 Date CoC: 2014-05-24
Date month: 2014-05-24 Date CoC: 2014-05-24
Date month: 2014-05-25 Date CoC: 2014-05-24
Date month: 2014-05-26 Date CoC: 2014-05-24
Date month: 2014-05-27 Date CoC: 2014-05-24
Date month: 2014-05-28 Date CoC: 2014-05-24
Date month: 2014-05-29 Date CoC: 2014-05-24
Date month: 2014-05-30 Date CoC: 2014-05-24
Date month: 2014-05-31 Date CoC: 2014-05-24
Date month: 2014-06-01 Date CoC: 2014-05-24
这是我的预期结果:
Date month: 2014-04-28 Date CoC: 2014-04-30
Date month: 2014-04-29 Date CoC: 2014-04-30
Date month: 2014-04-30 Date CoC: 2014-04-30
Date month: 2014-05-01 Date CoC: 2014-04-30
Date month: 2014-05-02 Date CoC: 2014-04-30
Date month: 2014-05-03 Date CoC: 2014-04-30
Date month: 2014-05-04 Date CoC: 2014-04-30
Date month: 2014-05-05 Date CoC: 2014-04-30
Date month: 2014-05-06 Date CoC: 2014-04-30
Date month: 2014-05-07 Date CoC: 2014-04-30
Date month: 2014-05-08 Date CoC: 2014-04-30
Date month: 2014-05-09 Date CoC: 2014-04-30
Date month: 2014-05-10 Date CoC: 2014-04-30
Date month: 2014-05-11 Date CoC: 2014-04-30
Date month: 2014-05-12 Date CoC: 2014-04-30
Date month: 2014-05-13 Date CoC: 2014-04-30
(change date CoC)
Date month: 2014-05-14 Date CoC: 2014-05-14
Date month: 2014-05-16 Date CoC: 2014-05-14
Date month: 2014-05-17 Date CoC: 2014-05-14
Date month: 2014-05-18 Date CoC: 2014-05-14
Date month: 2014-05-19 Date CoC: 2014-05-14
Date month: 2014-05-20 Date CoC: 2014-05-14
Date month: 2014-05-21 Date CoC: 2014-05-14
Date month: 2014-05-22 Date CoC: 2014-05-14
Date month: 2014-05-23 Date CoC: 2014-05-14
(change date CoC)
Date month: 2014-05-24 Date CoC: 2014-05-24
Date month: 2014-05-25 Date CoC: 2014-05-24
Date month: 2014-05-26 Date CoC: 2014-05-24
Date month: 2014-05-27 Date CoC: 2014-05-24
Date month: 2014-05-28 Date CoC: 2014-05-24
Date month: 2014-05-29 Date CoC: 2014-05-24
Date month: 2014-05-30 Date CoC: 2014-05-24
Date month: 2014-05-31 Date CoC: 2014-05-24
非常感谢!! :)
答案 0 :(得分:1)
您的嵌套if将始终为false,因此将应用其他内容;因此count_coc ++,这就是为什么在两次运行后打印CoC的最后一个元素: 你有:
if ((count_coc) < (dtJs.size()-1)) {
//other code
if ((count_coc) > dtJs.size()) //this will always be false