import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 獲取兩個時間之間的間隔天數
* @param startTimeStr 開始時間
* @param endTimeStr 結束時間
* @return 天數 例如2018-11-01 00:00:00至2018-11-30 23:59:59 返回為30
*/
public static String getBetweenDays(String startTimeStr, String endTimeStr) {
int betweenDays = 0;
Date startTime = strToDateLong(startTimeStr);
Date endTime = strToDateLong(endTimeStr);
long start = startTime.getTime();
long end = endTime.getTime();
betweenDays = (int) (Math.abs(end - start)/(24*3600*1000));
return String.valueOf(betweenDays + 1);
}
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public static void main(String[] args) {
System.err.println(getBetweenDays( "2018-11-01 00:00:00", "2018-11-29 01:59:59"));
}