我编写了一个程序,它总是计算两个日期之间的天数差异。我现在想总结所有差异天数,并将其除以计数,以便得到平均天数。有人能帮我吗?有没有办法甚至避免 for 循环。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AVGDateCalculation {
public static void main (String [] args) throws ParseException{
String formatted =
"2014-04-28 ,2014-04-28 ,"
+ "2015-10-26 ,2015-10-30 ,"
+ "2015-07-30 ,2015-07-30 ,"
+ "2015-04-14 ,2015-04-20 ,"
+ "2013-11-14 ,2013-11-18 ,"
+ "2014-04-16 ,2014-04-22 ,"
+ "2014-11-19 ,2014-11-21 ,"
+ "2019-10-01 ,2019-10-01 ";
String[] parts = formatted.split(",");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
int count = 0;
int a;
long difference = 0L;
float daysBetween = 0.00f;
float averageDays = 0.00f;
Date dateBefore = null;
Date dateAfter = null;
for (a = 0; a<parts.length; a+=1) {
dateBefore = sdf.parse(parts[a++]);
count++;
dateAfter = sdf.parse(parts[a+=0]);
difference = dateAfter.getTime() - dateBefore.getTime();
daysBetween = (difference / (1000*60*60*24));
averageDays = (count / daysBetween);
System.out.println(String.valueOf(dateAfter) + " - " + `String.valueOf(dateBefore));`
System.out.println(String.valueOf(dateAfter.getTime()) + " - " + String.valueOf(dateBefore.getTime()));
System.out.println(String.valueOf(daysBetween));
System.out.println(String.valueOf(averageDays) + " days");
System.out.println(String.valueOf(count));
}
}
}
江户川乱折腾
哈士奇WWW
相关分类