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;
}
日期区间分割算法
本文介绍了一种用于将两个指定日期之间的区间按天和按小时进行均匀分割的算法。该算法能够确保从开始日期到结束日期生成一系列等间隔的时间点,并且能够处理开始日期和结束日期之间的有效性检查。
381

被折叠的 条评论
为什么被折叠?



