我试图找出几个日期之间最大的差异,那怎么可能?答案应为01012001-01012011。我试图找到两个列表之间最大的日期差距,那怎么可能?
public void Date (){
SimpleDateFormat myFormat = new SimpleDateFormat("ddMMyyyy");
List<String> start = Arrays.asList("01011992","01012001","01012001");
String dateBeforeString = start.get(2);
List<String> end = Arrays.asList("02012001","02012001","01012011");
String dateAfterString = end.get(2);
try {
Date dateBefore = myFormat.parse(dateBeforeString);
Date dateAfter = myFormat.parse(dateAfterString);
long difference = dateAfter.getTime() - dateBefore.getTime();
daysBetween = (difference / (1000*60*60*24));
System.out.println("Days: "+daysBetween);
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
尝试这种方式:
SimpleDateFormat myFormat = new SimpleDateFormat("ddMMyyyy");
List<String> start = Arrays.asList("01011992", "01012001", "01012001");
List<String> end = Arrays.asList("02012001", "02012001", "01012011");
int lastIndex =0;
long lastValue =0;
for (int i = 0; i < start.size(); i++) {
String dateBeforeString = start.get(i);
String dateAfterString = end.get(i);
try {
Date dateBefore = myFormat.parse(dateBeforeString);
Date dateAfter = myFormat.parse(dateAfterString);
long difference = dateAfter.getTime() - dateBefore.getTime();
long daysBetween = (difference / (1000 * 60 * 60 * 24));
if(lastValue<daysBetween){
lastValue=daysBetween;
lastIndex = i;
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("result: " + start.get(lastIndex)+"-"+end.get(lastIndex)+" maxDiff "+lastValue);