输入开始时间和结束时间获取之间相隔的天数,月数,年数:
已经验证!
//月开始时间和结束时间之间相隔天数
public static void main(String[] args) throws Exception {
List list = getMonthBetween("2017-01-01", "2017-02-01");
for (Object object : list) {
System.out.println(object);
}
}
private static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(minDate));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(maxDate));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);//可以从天数用:DAY_OF_YEAR 月数MONTH 年数YEAR
}
return result;
}