首页 > 其他分享 >SpringBoot获取Cron表达式当天第一次执行时间

SpringBoot获取Cron表达式当天第一次执行时间

时间:2022-11-24 19:37:14浏览次数:43  
标签:DateUtils SpringBoot zdt 00 Cron Date 表达式

//不废话,直接干

CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator("0 15 9,21 * * ?");
Date date = DateUtils.toDate(LocalDate.now()); Date yesterday = new Date(date.getTime() - 1000L); //昨天最后一秒的下一个时次,也就是今天第一个时次 Date firstCronTimeToday = cronSequenceGenerator.next(yesterday); System.out.println(firstCronTimeToday);

 

 

 为什么要减1000L呢?

因为Cron表达式最小单位是秒,因此需要先获取到今天00:00:00,然后减1秒,从昨天23:59:59开始计算。

 

 

 

其中DateUtils的代码如下

public class DateUtils {

public static Date toDate(LocalDate localDate){
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(0, 0, 0)); 
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
}

最后看下quartz包是怎么实现的

 

 其实也是这么干的,为什么我没有用它的API,因为不知道它这API咋用的,就看了下原理。

标签:DateUtils,SpringBoot,zdt,00,Cron,Date,表达式
From: https://www.cnblogs.com/yanyan-rourou/p/16922971.html

相关文章

  • springboot与数据访问整合mybatis与springData JPA
    1、jdbc<dependency><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐starter‐jdbc</artifactId></dependency><dependency><groupId>......
  • Java——多线程:Lamda表达式
    多线程理解继承Thread类子类继承Thread类具备多线程能力启动线程:子类对象.start()不建议使用:避免oop单继承局限性实现Runnable接口实现接口Runnable具有多......
  • springboot启动配置原理
    几个重要的事件回调机制配置在META-INF/spring.factoriesApplicationContextInitializerSpringApplicationRunListener只需要放在ioc容器中ApplicationRunnerCommandL......
  • springboot整合视图层+持久层技术
    整合持久层技术整合jsp整合frameworker整合thymeleaf整合jsp技术pom文件<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchem......
  • springboot+vue+element-ui实现文件上传前后台代码+文件上传后台工具类
    //新增模板接口@PreAuthorize("@ss.hasPermi('pshdhx:template:add')")@Log(title="template",businessType=BusinessType.INSERT)@PostMapping("add")@ResponseBodypublic......
  • springboot初步整合web(一)
    1、整合filterspringboot-整合filter方式1@WebFilter(filternameurlpattern="拦截多个请求,{"*.do","*.action","/firstServlet",}")publicclsssFirstFilterimplementsF......
  • springboot 与 k8s结合使用
    https://juejin.cn/post/7138975184114941965https://techdozo.dev/deploying-a-restful-spring-boot-microservice-on-kubernetes/https://piotrminkowski.com/2017/05/......
  • springboot打包包含lib文件夹的jar包
    1、添加dependency<dependency><groupId>com.tyilack</groupId><artifactId>test</artifactId><version>4.4</version><scope>system</scope><systemPa......
  • SSM和Springboot中的分页实现
    SpringBoot实现引入依赖<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.1.2</version></depen......
  • springboot 项目打为war包
    1.springboot项目打包配置:首先在启动类目录下新增打包类  启动类继承自SpringBootServletInitializer方可正常部署至常规tomcat下,其主要能够起到web.xml的作用(web.xm......