package com.jake.singlecandy.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
* @param startTime 开始时间(格式为字符串yyyy-MM-dd HH:mm:ss)
* @param endTime 结束时间(格式为字符串yyyy-MM-dd HH:mm:ss)
* @param calUnit 间隔单位,Calendar类里的常量,年月周日时分秒
* @param interval 间隔大小,每隔多久获取一次时间
* @return 返回时间集合(格式为字符串yyyy-MM-dd HH:mm:ss)
* @throws ParseException
*/
public static ArrayList<String> getIntervals(String startTime, String endTime, int calUnit, int interval) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ArrayList<String> times = new ArrayList<>();
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
times.add(startTime);
while (startCal.getTime().before(endDate)) {
startCal.add(calUnit, interval);
times.add(sdf.format(startCal.getTime()));
}
return times;
}
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/19614.html