当前位置:网站首页>不同时间类型的执行计划计算
不同时间类型的执行计划计算
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
边栏推荐
- 多线程 @Async 线程池
- Yarn online dynamic resource tuning
- PATH环境变量
- 基于ibeacons三点定位(微信小程序)
- Basic knowledge learning record
- SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
- Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
- STM32学习记录0007——新建工程(基于寄存器版)
- request模块
- Check in system based on ibeacons
猜你喜欢
基于微信小程序的wifi模块使用
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
Choreographer full resolution
微信小程序调用客服接口
JDBC入门
MYSQL 主从同步避坑版教程
记录一个奇怪的bug:缓存组件跳转之后出现组件复制
visio安装报错 1:1935 2:{XXXXXXXX...
Autumn recruitment in 2021, salary ranking No
Kettle -- control parsing
随机推荐
DeepinV20安装Mariadb
There is a mining virus in the server
Restful WebService和gSoap WebService的本质区别
Spark入门基本操作
收藏博客贴
基於CM管理的CDH集群集成Phoenix
mysql通过binlog文件恢复数据
Intégration de Clusters CDH Phoenix basée sur la gestion cm
项目中遇到的问题(五)操作Excel接口Poi的理解
switch使用(微信小程序)
SQL数据库
理解虚基类、虚函数与纯虚函数的概念(转)
RobotFramework 之 项目框架
腾讯根据ip解析地址
SPC简介
visio安装报错 1:1935 2:{XXXXXXXX...
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
freeCodeCamp----arithmetic_ Arranger exercise
基于ibeacons三点定位(微信小程序)
smart-doc + torna生成接口文档