计算两个时间的所有天数:
/**
* 获取两个时间段的内的所有日期(返回字符串)
* @param start
* @param end
* @return
*/
public static List<String> getBetweenDates(String start, String end) {
List<String> result = new ArrayList<String>();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date start_date = sdf.parse(start);
Date end_date = sdf.parse(end);
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(start_date);
Calendar tempEnd = Calendar.getInstance();
tempEnd.setTime(end_date);
while (tempStart.before(tempEnd)||tempStart.equals(tempEnd)) {
result.add(sdf.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
Collections.reverse(result);
return result;
}
/**
* 获取两个时间段的内的所有日期 (返回date)
* @param dBegin
* @param dEnd
* @return
*/
public static List<Date> findDates(Date dBegin, Date dEnd) {
List<Date> lDate = new ArrayList<Date>();
lDate.add(dBegin);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
lDate.add(calBegin.getTime());
}
return lDate;
}
计算两个时间所有年数:
/**
* 计算两个日期之间包含的所有年份
*
* @param beginTime
* @param endTime
* @return
*/
public static List<String> findYearsStr(String beginTime, String endTime) {
List<String> yearsStrList = new ArrayList<>();
beginTime = beginTime.substring(0, 4);
endTime = endTime.substring(0, 4);
if (beginTime.equals(endTime)) {
yearsStrList.add(beginTime);
} else {
yearsStrList.add(beginTime);
for (int i = 1; i <= Integer.parseInt(endTime) - Integer.parseInt(beginTime); i++) {
yearsStrList.add(String.valueOf(Integer.parseInt(beginTime) + i));
}
}
return yearsStrList;
}
}
计算两个时间包含的月份:
/**
* 计算两个日期之间包含的所有月份
*
* @param beginTime
* @param endTime
* @return
* @throws ParseException
*/
public static List<String> findMonthsStr(String beginTime, String endTime) throws ParseException {
List<String> monthsStrList = new ArrayList<>();
//格式化为年月
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(beginTime));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(endTime));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
while (min.before(max)) {
monthsStrList.add(sdf.format(min.getTime()));
min.add(Calendar.MONTH, 1);
}
return monthsStrList;
}
计算两个时间相差的分钟数:
/**
* 计算两个时间相差分钟数
* @param beginTime
* @param endTime
* @return
*/
public static long findMin(String beginTime, String endTime){
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date begin = dfs.parse(beginTime);
Date end = dfs.parse(endTime);
long between = (end.getTime() - begin.getTime())/1000;
long min = between/60;
return min;
}catch (Exception e){
e.printStackTrace();
}
return 0;
}
计算两个时间相差小时:
/**
* 计算两个时间相差小时
* @param beginTime
* @param endTime
* @return
*/
public static long findHour(String beginTime, String endTime){
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date begin = dfs.parse(beginTime);
Date end = dfs.parse(endTime);
long between = (end.getTime() - begin.getTime())/1000;
long min = between/3600;
return min;
}catch (Exception e){
e.printStackTrace();
}
return 0;
}