当前位置:网站首页>不同时间类型的执行计划计算
不同时间类型的执行计划计算
2022-04-23 14:03:00 【洒了脱】
1.问题描述:
待叙。。。
2.代码:
public class DateTimeUtil {
private static final DateTimeFormatter DAY_FULL_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/** * 按类型和起始时间获取所有执行时间列表 * @param startTime 开始时间 * @param endTime 结束时间 * @param type 类型: day(天), week(周), month(月), quarter(季度), year(年) * @return 执行日期列表 */
public static List<String> getExecuteDaysByType(String type, LocalDateTime startTime, LocalDateTime endTime) {
LocalDate startDate = startTime.toLocalDate(), endDate = endTime.toLocalDate();
LocalDateTime now = LocalDateTime.now();
LocalDate today = now.toLocalDate();
if("day".equalsIgnoreCase(type)){
if(startDate.isBefore(today)){
startDate = today;
}
return getExecDays(startDate, endDate, now, date -> date.plusDays(1));
}
if("week".equalsIgnoreCase(type)){
//开始时间对应的周一
startDate = startDate.minusDays(startDate.getDayOfWeek().getValue()-1);
//本周一
LocalDate currMonday = today.minusDays(today.getDayOfWeek().getValue()-1);
if(startDate.isBefore(currMonday)){
startDate = currMonday;
}
return getExecDays(startDate, endDate, now, date -> date.plusDays(7));
}
if("month".equalsIgnoreCase(type)){
//开始时间当月1号
startDate = LocalDate.of(startDate.getYear(), startDate.getMonthValue(), 1);
//本月1号
LocalDate currMonth = LocalDate.of(today.getYear(), today.getMonthValue(), 1);
if(startDate.isBefore(currMonth)){
startDate = currMonth;
}
return getExecDays(startDate, endDate, now, date -> date.plusMonths(1));
}
if("quarter".equalsIgnoreCase(type)){
//开始时间当季度首月1号
startDate = LocalDate.of(startDate.getYear(), startDate.getMonth().firstMonthOfQuarter(), 1);
//本季度首月1号
LocalDate currQuarter = LocalDate.of(today.getYear(), today.getMonth().firstMonthOfQuarter(), 1);
if(startDate.isBefore(currQuarter)){
startDate = currQuarter;
}
return getExecDays(startDate, endDate, now, date -> date.plusMonths(3));
}
if("year".equalsIgnoreCase(type)){
//开始时间当年首月1号
startDate = LocalDate.of(startDate.getYear(), 1, 1);
//本年度首月1号
LocalDate currYear = LocalDate.of(today.getYear(), 1, 1);
if(startDate.isBefore(currYear)){
startDate = currYear;
}
return getExecDays(startDate, endDate, now, date -> date.plusYears(1));
}
return Collections.emptyList();
}
private static List<String> getExecDays(LocalDate startDate, LocalDate endDate, LocalDateTime now, Function<LocalDate, LocalDate> timePlus){
LocalDate today = now.toLocalDate();
List<String> execDays = new ArrayList<>();
while(!startDate.isAfter(endDate)){
if(!startDate.isAfter(today)){
//执行时间在今天及今天以前,则在当前时间5分钟后执行
execDays.add(now.plusMinutes(5).format(DAY_FULL_FMT));
}else{
execDays.add(startDate.atStartOfDay().format(DAY_FULL_FMT));
}
//下一个执行日期
startDate = timePlus.apply(startDate);
}
return execDays;
}
}
4.Test:
public static void main(String[] args) {
LocalDateTime startTime = LocalDateTime.of(2020, 7, 1, 12, 0, 0);
LocalDateTime endTime = LocalDateTime.of(2022, 6, 1, 0, 0, 0);
System.out.println(DateTimeUtil.getExecuteDaysByType("year", startTime, endTime));
}
版权声明
本文为[洒了脱]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xhom_w/article/details/119641651
边栏推荐
- switch使用(微信小程序)
- POI operation word template replaces data and exports word
- 蓝绿发布、滚动发布、灰度发布,有什么区别?
- Windos中安装labellmg教程
- SPC简介
- 微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
- Taobao released the baby prompt "your consumer protection deposit is insufficient, and the expiration protection has been started"
- visio安装报错 1:1935 2:{XXXXXXXX...
- 关于stream流,浅记一下------
- Pytorch 经典卷积神经网络 LeNet
猜你喜欢
微信小程序获取登录用户信息、openid和access_token
Nifi 快速安装及文件同步操作
基于CM管理的CDH集群集成Phoenix
关于pthread多线程一些好文章
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
Chapter I review of e-commerce spike products
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
RobotFramework 之 文件上传和下载
As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
mysql新表,自增id长达20位,原因竟是......
随机推荐
Spark入门基本操作
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
微信小程序 input隐藏和不可操作的设置
浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证
BUG_me
对List集合进行分页
Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
Problems encountered in the project (V) understanding of operating excel interface poi
Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)
Chrome插件 之 Selenium IDE、XPath 安装
scikit-learn构建模型的万能模板
微信小程序获取登录用户信息、openid和access_token
Windos中安装labellmg教程
groutine
centOS下mysql主从配置
Interesting talk about network protocol
JDBC入门
1256: bouquet for algenon
9月8日,临去松山湖的前夜
Chapter I review of e-commerce spike products