Cron表达式通常用于执行一些定时任务,在本篇文章中,暂时不会记录如何根据Cron表达式来执行一些定时任务。本章主要的目的是根据Cron表达式,通过Spring自带的CronSequenceGenerator 类获取下次执行时间,因为有时在项目中,需要展示定时任务的上次执行时间、下次执行时间和下下次执行时间。下次执行时间和下下次执行时间可以通过代码来获取,至于上次执行时间就需要在定时任务执行时记录执行的时间,再在页面上显示。话不多数,下面的代码就是就是获取下次执行时间和下下次执行时间的代码。

String cron="0 0/10 * * * ? ";  //每10分钟执行一次
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron);
Date currentTime = new Date(); //当前系统时间
Date nextTimePoint = cronSequenceGenerator.next(currentTime); //下次执行时间
Date nextNextTimePoint = cronSequenceGenerator.next(nextTimePoint); //下下次执行时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=sdf.format(currentTime);
String nextTime=sdf.format(nextTimePoint);
String nextDoubleTme=sdf.format(nextNextTimePoint);
System.out.println("当前系统时间为:"+time);
System.out.println("下次执行时间为:"+nextTime);
System.out.println("下下次执行时间为:"+nextDoubleTme);

下面就是上面代码的输出结果

当前系统时间为:2018-05-13 22:32:12
下次执行时间为:2018-05-13 22:40:00
下下次执行时间为:2018-05-13 22:50:00

根据Cron表达式,通过Spring自带的CronSequenceGenerator类获取下次执行时间的更多相关文章

  1. cron 获取下次运行时间(基于 C# + Quartz.NET)

    代码 Quartz 的 cron 支持秒,导致一些 cron 库无法准确的获得下次执行时间,这里使用 Quartz.Net 自带的方法来获取下次执行时间. //引用 Quartz CronExpres ...

  2. spring 缓存(spring自带Cache)(入门)源码解读

    spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...

  3. spring 缓存(spring自带Cache)(入门)

    spring的缓存机制,是方法纬度的缓存机制, 这就意味着我们并不用关注 底层是否使用了数据库以及通过什么方式访问的数据库: 因此,此缓存方法既适用于dao层,也适用于service层. spring ...

  4. spring cron表达式及解析过程

    1.cron表达式 cron表达式是用来配置spring定时任务执行时间的字符串,由5个空格分隔成的6个域构成,格式如下: {秒}  {分}  {时}  {日}  {月}  {周} 每一个域的含义解释 ...

  5. 摆脱Spring 定时任务的@Scheduled cron表达式的困扰

    一.背景 最近因为需要,需要适用Spring的task定时任务进行跑定时任务,以前也接触过,但是因为懒没有好好地理解@Scheduled的cron表达式,这次便对它做了一个全方位的了解和任务,记录下来 ...

  6. Spring cron 表达式

    前言: 最近做的项目有用到定时器,每周只在特定时间运行一次,考虑到Spring Task的简单易用性,就果断选择了,我是配置在配置文件里面,没有用注解@Scheduled,推荐配置,注解虽方便,但更改 ...

  7. (2)Spring集成Quartz定时任务框架介绍和Cron表达式详解

    在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...

  8. Spring集成Quartz定时任务框架介绍和Cron表达式详解

    原文地址:http://www.cnblogs.com/obullxl/archive/2011/07/10/spring-quartz-cron-integration.html 在JavaEE系统 ...

  9. Spring 定时任务之 @Scheduled cron表达式

    一个基于Spring boot的一个demo: Java配置中开户对Scheduled的支持 import org.springframework.context.annotation.Configu ...

随机推荐

  1. hadoop.io.native.NativeID$Windows.access0 报错问题解决

    系统:win10 hadoop-2.6.0版本 java:1.8 版本32位   wordcount在本地运行时报错: Exception in thread "main" jav ...

  2. jq-在线引入

    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script><sc ...

  3. jq-demo-点击选择(英雄联盟)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 牛客多校第三次B——线段树维护线性基交

    写线性基交函数时调试了半天.. #include<bits/stdc++.h> using namespace std; #define ll long long #define maxn ...

  5. NX二次开发-NXOPEN创建工程图表格Annotations::TableSectionBuilder *tableSectionBuilder1;

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  6. 让nginx支持patchinfo,(支持codeigniter,thinkphp,ZF等框架)

    nginx 的config配置: server { listen ; server_name xxx; ....if (!-e $request_filename) { rewrite ^/(.*)$ ...

  7. python virtual env 使用 jupyter ipython notebook,舒服了, 工作效率翻倍

    话不多说,尊重原作者 知乎链接

  8. faster-rcnn代码阅读-roi-data层

    这一节讲述roi-data层,和这一层有关的结构图如下: roi-data层的prototxt定义如下: layer { name: 'roi-data' type: 'Python' bottom: ...

  9. Java-javaFx库运用-时钟显示

    JavaFx是开发Java GUI程序的新框架.JavaFX应用可以无缝地在桌面或web浏览器中运行.具有内建的2D.3D动画支持,以及视频和音频的回放功能,可以作为一个应用独立运行或者在浏览器中运行 ...

  10. 删除csdn自己上传的资源

    原文地址:http://www.xuebuyuan.com/1875216.html 昨天晚上进行测试,上传了一个压缩包和大家分享,测试完成后,为了不想给被测试的公司造成伤害,决定把上传的包删除,结果 ...