Java_获取两个日期中间的日期

来源:互联网 发布:mac命令行怎么打开 编辑:程序博客网 时间:2024/06/05 08:03
yyyyMMdd形式
private List<Date> dateSplit(Date startDate, Date endDate)              throws Exception {        if (!startDate.before(endDate)&&startDate.compareTo(endDate)==1){              throw new Exception("开始时间应该在结束时间之后");          }                        Long spi = endDate.getTime() - startDate.getTime();          Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数            List<Date> dateList = new ArrayList<Date>();          dateList.add(startDate);          for (int i = 1; i <= step; i++) {              dateList.add(new Date(dateList.get(i - 1).getTime()                      + (24 * 60 * 60 * 1000)));// 比上一天+一          }          return dateList;      }


yyyyMMddHH形式


private List<Date> dateHHMMSplit(Date startDate, Date endDate)              throws Exception {        if (!startDate.before(endDate)&&startDate.compareTo(endDate)==1){              throw new Exception("开始时间应该在结束时间之后");          }                        Long spi = endDate.getTime() - startDate.getTime();          Long step = spi / (10 * 60 * 1000);// 相隔小时数           List<Date> dateList = new ArrayList<Date>();          dateList.add(startDate);          for (int i = 1; i <= step; i++) {              dateList.add(new Date(dateList.get(i - 1).getTime()                      + (10 * 60 * 1000)));// 加1小时        }          return dateList;      }


原创粉丝点击