两个时间按照固定区间切割:
package com.xn.bmpos.manager.bmposmanager;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @Author: zly
* @Date: 2019/6/2 17:08
*/
public class DateUtil {
/**
* 切割時間段
*
* @param dateType 交易類型 M/D/H/m/s -->每月/每天/每小時/每分鐘
* @param start yyyy-MM-dd HH:mm:ss
* @param end yyyy-MM-dd HH:mm:ss
* @return
*/
public static List<String> cutDate(String dateType, String start, String end) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dBegin = sdf.parse(start);
Date dEnd = sdf.parse(end);
return findDates(dateType, dBegin, dEnd);
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public static List<String> findDates(String dateType, Date dBegin, Date dEnd) throws Exception {
List<String> listDate = new ArrayList<>();
Calendar calBegin = Calendar.getInstance();
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
calEnd.setTime(dEnd);
while (calEnd.after(calBegin)) {
switch (dateType) {
case "M":
calBegin.add(Calendar.MONTH, 1);
break;
case "D":
calBegin.add(Calendar.DAY_OF_YEAR, 1);break;
case "H":
calBegin.add(Calendar.HOUR, 1);break;
case "m":
calBegin.add(Calendar.MINUTE, 15);break;
case "S":
calBegin.add(Calendar.SECOND, 1);break;
}
if (calEnd.after(calBegin))
listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calBegin.getTime()));
else
listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calEnd.getTime()));
}
return listDate;
}
}