计算字符串日期之间的天数差
private int diffDate(String startDate, String endDate) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
Date date1;
Date date2;
try {
date1 = simpleDateFormat.parse(startDate);
date2 = simpleDateFormat.parse(endDate);
calendar1.setTime(date1);
calendar2.setTime(date2);
} catch (ParseException e) {
e.printStackTrace();
}
long time1 = calendar1.getTimeInMillis();
long time2 = calendar2.getTimeInMillis();
return (int) ((time2 - time1) / (1000 * 3600 * 24));
}
测试:
@Test
public void test18() throws Exception {
String a = "2020-02-01";
String b = "2020-02-31";
int diff = diffDate(a, b);
System.out.println(diff);
}