Javaコンポーネント:2つの時間の間隔を取得する日数
1126 ワード
/**
* @Desc
* @param startTimeStr
* @param endTimeStr
* @return
*/
protected 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);
}
protected 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;
}
private Calendar getDate(String dateStr) {
Date startDate = null;
try {
startDate = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid start date, startDateStr=" + dateStr);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
return calendar;
}