public static List<String> findDates(String beginTime, String endTime) throws ParseException { List<String> allDate = new ArrayList(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dBegin = sdf.parse(beginTime); Date dEnd = sdf.parse(endTime); allDate.add(sdf.format(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); allDate.add(sdf.format(calBegin.getTime())); } System.out.println("时间==" + allDate); return allDate; }
计算两个时间之间的所有天数
最新推荐文章于 2023-07-21 12:08:26 发布