导读
由于项目需要,使用的是Timer类来实现定时任务。所以这里简单描述下其使用技巧。
使用
通过设置启动延迟毫秒
CODE
package com.heioky.heiokyadmin.controller;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* 定时任务,定时处理数据
*/
public class Tm extends TimerTask {
/**
* 每次间隔执行的时间,单位ms
*/
private static long period = 1000;
/**
* 首次执行的时间,也就延迟多少秒开始执行这个定时任务
*/
private static long delay = 2000;
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
new Tm().start();
}
public void start() {
Timer timer = new Timer();
System.out.println(dateFormat.format(new Date()));
timer.schedule(this, delay, period);
}
@Override
public void run() {
System.out.println(">>>" + dateFormat.format(new Date()));
}
}
运行结果
"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" -javaagent:E:\idea20200201\lib\idea_rt.jar=53380:E:\idea20200201\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;E:\mypersionworkspace\heioky-admin\target\classes;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-web\2.2.6.RELEASE\spring-boot-starter-web-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter\2.2.6.RELEASE\spring-boot-starter-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-logging\2.2.6.RELEASE\spring-boot-starter-logging-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-json\2.2.6.RELEASE\spring-boot-starter-json-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.3\jackson-datatype-jdk8-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.3\jackson-datatype-jsr310-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.3\jackson-module-parameter-names-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-tomcat\2.2.6.RELEASE\spring-boot-starter-tomcat-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\tomcat\embed\tomcat-embed-core\9.0.33\tomcat-embed-core-9.0.33.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\tomcat\embed\tomcat-embed-el\9.0.33\tomcat-embed-el-9.0.33.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.33\tomcat-embed-websocket-9.0.33.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-validation\2.2.6.RELEASE\spring-boot-starter-validation-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-web\5.2.5.RELEASE\spring-web-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-webmvc\5.2.5.RELEASE\spring-webmvc-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-thymeleaf\2.2.6.RELEASE\spring-boot-starter-thymeleaf-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\thymeleaf\thymeleaf-spring5\3.0.11.RELEASE\thymeleaf-spring5-3.0.11.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\thymeleaf\thymeleaf\3.0.11.RELEASE\thymeleaf-3.0.11.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\attoparser\attoparser\2.0.5.RELEASE\attoparser-2.0.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\unbescape\unbescape\1.1.6.RELEASE\unbescape-1.1.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\thymeleaf\extras\thymeleaf-extras-java8time\3.0.4.RELEASE\thymeleaf-extras-java8time-3.0.4.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-test\2.2.6.RELEASE\spring-boot-starter-test-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-test\2.2.6.RELEASE\spring-boot-test-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-test-autoconfigure\2.2.6.RELEASE\spring-boot-test-autoconfigure-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\minidev\json-smart\2.3\json-smart-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\jupiter\junit-jupiter-params\5.5.2\junit-jupiter-params-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\vintage\junit-vintage-engine\5.5.2\junit-vintage-engine-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\junit\junit\4.12\junit-4.12.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mockito\mockito-junit-jupiter\3.1.0\mockito-junit-jupiter-3.1.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\assertj\assertj-core\3.13.2\assertj-core-3.13.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mockito\mockito-core\3.1.0\mockito-core-3.1.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\bytebuddy\byte-buddy\1.10.8\byte-buddy-1.10.8.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\bytebuddy\byte-buddy-agent\1.10.8\byte-buddy-agent-1.10.8.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\objenesis\objenesis\2.6\objenesis-2.6.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-core\5.2.5.RELEASE\spring-core-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-jcl\5.2.5.RELEASE\spring-jcl-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-test\5.2.5.RELEASE\spring-test-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\xmlunit\xmlunit-core\2.6.4\xmlunit-core-2.6.4.jar;E:\offices\apache-maven-3.6.3\RepoHome\io\jsonwebtoken\jjwt\0.7.0\jjwt-0.7.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\core\jackson-databind\2.10.3\jackson-databind-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\core\jackson-annotations\2.10.3\jackson-annotations-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\core\jackson-core\2.10.3\jackson-core-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\alibaba\druid-spring-boot-starter\1.1.10\druid-spring-boot-starter-1.1.10.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\alibaba\druid\1.1.10\druid-1.1.10.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-autoconfigure\2.2.6.RELEASE\spring-boot-autoconfigure-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.3.2\mybatis-spring-boot-starter-1.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-jdbc\2.2.6.RELEASE\spring-boot-starter-jdbc-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\zaxxer\HikariCP\3.4.2\HikariCP-3.4.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-jdbc\5.2.5.RELEASE\spring-jdbc-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.3.2\mybatis-spring-boot-autoconfigure-1.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\alibaba\fastjson\1.2.37\fastjson-1.2.37.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-data-jpa\2.2.6.RELEASE\spring-boot-starter-data-jpa-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-aop\2.2.6.RELEASE\spring-boot-starter-aop-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\persistence\jakarta.persistence-api\2.2.3\jakarta.persistence-api-2.2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\transaction\jakarta.transaction-api\1.3.3\jakarta.transaction-api-1.3.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\hibernate\hibernate-core\5.4.12.Final\hibernate-core-5.4.12.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\javassist\javassist\3.24.0-GA\javassist-3.24.0-GA.jar;E:\offices\apache-maven-3.6.3\RepoHome\antlr\antlr\2.7.7\antlr-2.7.7.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\jboss\jandex\2.1.1.Final\jandex-2.1.1.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\dom4j\dom4j\2.1.1\dom4j-2.1.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\hibernate\common\hibernate-commons-annotations\5.1.0.Final\hibernate-commons-annotations-5.1.0.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\glassfish\jaxb\jaxb-runtime\2.3.2\jaxb-runtime-2.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\glassfish\jaxb\txw2\2.3.2\txw2-2.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\sun\istack\istack-commons-runtime\3.0.8\istack-commons-runtime-3.0.8.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\jvnet\staxex\stax-ex\1.8.1\stax-ex-1.8.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\sun\xml\fastinfoset\FastInfoset\1.2.16\FastInfoset-1.2.16.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\data\spring-data-jpa\2.2.6.RELEASE\spring-data-jpa-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\data\spring-data-commons\2.2.6.RELEASE\spring-data-commons-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-orm\5.2.5.RELEASE\spring-orm-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-tx\5.2.5.RELEASE\spring-tx-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-aspects\5.2.5.RELEASE\spring-aspects-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\security\spring-security-core\5.2.2.RELEASE\spring-security-core-5.2.2.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-aop\5.2.5.RELEASE\spring-aop-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-beans\5.2.5.RELEASE\spring-beans-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-context\5.2.5.RELEASE\spring-context-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-expression\5.2.5.RELEASE\spring-expression-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\github\pagehelper\pagehelper-spring-boot-starter\1.2.5\pagehelper-spring-boot-starter-1.2.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\github\pagehelper\pagehelper-spring-boot-autoconfigure\1.2.5\pagehelper-spring-boot-autoconfigure-1.2.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\github\pagehelper\pagehelper\5.1.4\pagehelper-5.1.4.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\github\jsqlparser\jsqlparser\1.0\jsqlparser-1.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\baomidou\mybatis-plus\2.3\mybatis-plus-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\baomidou\mybatis-plus-support\2.3\mybatis-plus-support-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\baomidou\mybatis-plus-core\2.3\mybatis-plus-core-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\baomidou\mybatis-plus-generate\2.3\mybatis-plus-generate-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\projectlombok\lombok\1.18.12\lombok-1.18.12.jar;E:\offices\apache-maven-3.6.3\RepoHome\commons-beanutils\commons-beanutils\1.8.0\commons-beanutils-1.8.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\sf\ezmorph\ezmorph\1.0.6\ezmorph-1.0.6.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-devtools\2.2.6.RELEASE\spring-boot-devtools-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot\2.2.6.RELEASE\spring-boot-2.2.6.RELEASE.jar" com.heioky.heiokyadmin.controller.Tm
2020-12-09 09:50:41
>>>2020-12-09 09:50:43
>>>2020-12-09 09:50:44
>>>2020-12-09 09:50:45
>>>2020-12-09 09:50:46
>>>2020-12-09 09:50:47
>>>2020-12-09 09:50:48
>>>2020-12-09 09:50:49
结论
使用该方式来实现定时任务的方式,可以使用指定程序多少毫秒之后来执该任务。这种如果不确定具体使用时间,使用该方式较佳。
通过指定具体时间来实现
CODE
package com.heioky.heiokyadmin.controller;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
//每隔5分钟执行一次
public class Test extends TimerTask {
private static long expiry = 1000*60;
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
new Test().start();
}
public void start() {
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);//小時
calendar.set(Calendar.MINUTE, 52);//分钟
calendar.set(Calendar.SECOND, 0);//秒
Date date = calendar.getTime();
System.out.println(dateFormat.format(date));
if (date.before(new Date())) {
date = addDay(date, 2);
}
System.out.println(dateFormat.format(date));
timer.schedule(this, date, expiry);
}
public static Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.MINUTE, 5);
return startDT.getTime();
}
@Override
public void run() {
System.out.println(">>>"+dateFormat.format(new Date()));
}
}
运行结果
"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" -javaagent:E:\idea20200201\lib\idea_rt.jar=53650:E:\idea20200201\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;E:\mypersionworkspace\heioky-admin\target\classes;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-web\2.2.6.RELEASE\spring-boot-starter-web-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter\2.2.6.RELEASE\spring-boot-starter-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-logging\2.2.6.RELEASE\spring-boot-starter-logging-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\logging\log4j\log4j-to-slf4j\2.12.1\log4j-to-slf4j-2.12.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\logging\log4j\log4j-api\2.12.1\log4j-api-2.12.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\yaml\snakeyaml\1.25\snakeyaml-1.25.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-json\2.2.6.RELEASE\spring-boot-starter-json-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.10.3\jackson-datatype-jdk8-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.10.3\jackson-datatype-jsr310-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\module\jackson-module-parameter-names\2.10.3\jackson-module-parameter-names-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-tomcat\2.2.6.RELEASE\spring-boot-starter-tomcat-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\tomcat\embed\tomcat-embed-core\9.0.33\tomcat-embed-core-9.0.33.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\tomcat\embed\tomcat-embed-el\9.0.33\tomcat-embed-el-9.0.33.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.33\tomcat-embed-websocket-9.0.33.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-validation\2.2.6.RELEASE\spring-boot-starter-validation-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\hibernate\validator\hibernate-validator\6.0.18.Final\hibernate-validator-6.0.18.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-web\5.2.5.RELEASE\spring-web-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-webmvc\5.2.5.RELEASE\spring-webmvc-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-thymeleaf\2.2.6.RELEASE\spring-boot-starter-thymeleaf-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\thymeleaf\thymeleaf-spring5\3.0.11.RELEASE\thymeleaf-spring5-3.0.11.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\thymeleaf\thymeleaf\3.0.11.RELEASE\thymeleaf-3.0.11.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\attoparser\attoparser\2.0.5.RELEASE\attoparser-2.0.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\unbescape\unbescape\1.1.6.RELEASE\unbescape-1.1.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\thymeleaf\extras\thymeleaf-extras-java8time\3.0.4.RELEASE\thymeleaf-extras-java8time-3.0.4.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-test\2.2.6.RELEASE\spring-boot-starter-test-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-test\2.2.6.RELEASE\spring-boot-test-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-test-autoconfigure\2.2.6.RELEASE\spring-boot-test-autoconfigure-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\minidev\json-smart\2.3\json-smart-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\jupiter\junit-jupiter-params\5.5.2\junit-jupiter-params-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\vintage\junit-vintage-engine\5.5.2\junit-vintage-engine-5.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\junit\junit\4.12\junit-4.12.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mockito\mockito-junit-jupiter\3.1.0\mockito-junit-jupiter-3.1.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\assertj\assertj-core\3.13.2\assertj-core-3.13.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mockito\mockito-core\3.1.0\mockito-core-3.1.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\bytebuddy\byte-buddy\1.10.8\byte-buddy-1.10.8.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\bytebuddy\byte-buddy-agent\1.10.8\byte-buddy-agent-1.10.8.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\objenesis\objenesis\2.6\objenesis-2.6.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-core\5.2.5.RELEASE\spring-core-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-jcl\5.2.5.RELEASE\spring-jcl-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-test\5.2.5.RELEASE\spring-test-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\xmlunit\xmlunit-core\2.6.4\xmlunit-core-2.6.4.jar;E:\offices\apache-maven-3.6.3\RepoHome\io\jsonwebtoken\jjwt\0.7.0\jjwt-0.7.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\core\jackson-databind\2.10.3\jackson-databind-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\core\jackson-annotations\2.10.3\jackson-annotations-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\jackson\core\jackson-core\2.10.3\jackson-core-2.10.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\alibaba\druid-spring-boot-starter\1.1.10\druid-spring-boot-starter-1.1.10.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\alibaba\druid\1.1.10\druid-1.1.10.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-autoconfigure\2.2.6.RELEASE\spring-boot-autoconfigure-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.3.2\mybatis-spring-boot-starter-1.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-jdbc\2.2.6.RELEASE\spring-boot-starter-jdbc-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\zaxxer\HikariCP\3.4.2\HikariCP-3.4.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-jdbc\5.2.5.RELEASE\spring-jdbc-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.3.2\mybatis-spring-boot-autoconfigure-1.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\alibaba\fastjson\1.2.37\fastjson-1.2.37.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-data-jpa\2.2.6.RELEASE\spring-boot-starter-data-jpa-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-starter-aop\2.2.6.RELEASE\spring-boot-starter-aop-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\aspectj\aspectjweaver\1.9.5\aspectjweaver-1.9.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\persistence\jakarta.persistence-api\2.2.3\jakarta.persistence-api-2.2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\jakarta\transaction\jakarta.transaction-api\1.3.3\jakarta.transaction-api-1.3.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\hibernate\hibernate-core\5.4.12.Final\hibernate-core-5.4.12.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\jboss\logging\jboss-logging\3.4.1.Final\jboss-logging-3.4.1.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\javassist\javassist\3.24.0-GA\javassist-3.24.0-GA.jar;E:\offices\apache-maven-3.6.3\RepoHome\antlr\antlr\2.7.7\antlr-2.7.7.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\jboss\jandex\2.1.1.Final\jandex-2.1.1.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\dom4j\dom4j\2.1.1\dom4j-2.1.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\hibernate\common\hibernate-commons-annotations\5.1.0.Final\hibernate-commons-annotations-5.1.0.Final.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\glassfish\jaxb\jaxb-runtime\2.3.2\jaxb-runtime-2.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\glassfish\jaxb\txw2\2.3.2\txw2-2.3.2.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\sun\istack\istack-commons-runtime\3.0.8\istack-commons-runtime-3.0.8.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\jvnet\staxex\stax-ex\1.8.1\stax-ex-1.8.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\sun\xml\fastinfoset\FastInfoset\1.2.16\FastInfoset-1.2.16.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\data\spring-data-jpa\2.2.6.RELEASE\spring-data-jpa-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\data\spring-data-commons\2.2.6.RELEASE\spring-data-commons-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-orm\5.2.5.RELEASE\spring-orm-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-tx\5.2.5.RELEASE\spring-tx-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-aspects\5.2.5.RELEASE\spring-aspects-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\security\spring-security-core\5.2.2.RELEASE\spring-security-core-5.2.2.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-aop\5.2.5.RELEASE\spring-aop-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-beans\5.2.5.RELEASE\spring-beans-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-context\5.2.5.RELEASE\spring-context-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\spring-expression\5.2.5.RELEASE\spring-expression-5.2.5.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\github\pagehelper\pagehelper-spring-boot-starter\1.2.5\pagehelper-spring-boot-starter-1.2.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\github\pagehelper\pagehelper-spring-boot-autoconfigure\1.2.5\pagehelper-spring-boot-autoconfigure-1.2.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\github\pagehelper\pagehelper\5.1.4\pagehelper-5.1.4.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\github\jsqlparser\jsqlparser\1.0\jsqlparser-1.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\baomidou\mybatis-plus\2.3\mybatis-plus-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\baomidou\mybatis-plus-support\2.3\mybatis-plus-support-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\baomidou\mybatis-plus-core\2.3\mybatis-plus-core-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\com\baomidou\mybatis-plus-generate\2.3\mybatis-plus-generate-2.3.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\projectlombok\lombok\1.18.12\lombok-1.18.12.jar;E:\offices\apache-maven-3.6.3\RepoHome\commons-beanutils\commons-beanutils\1.8.0\commons-beanutils-1.8.0.jar;E:\offices\apache-maven-3.6.3\RepoHome\commons-collections\commons-collections\3.2.1\commons-collections-3.2.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;E:\offices\apache-maven-3.6.3\RepoHome\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;E:\offices\apache-maven-3.6.3\RepoHome\net\sf\ezmorph\ezmorph\1.0.6\ezmorph-1.0.6.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot-devtools\2.2.6.RELEASE\spring-boot-devtools-2.2.6.RELEASE.jar;E:\offices\apache-maven-3.6.3\RepoHome\org\springframework\boot\spring-boot\2.2.6.RELEASE\spring-boot-2.2.6.RELEASE.jar" com.heioky.heiokyadmin.controller.Tm
2020-12-09 09:54:13
>>>2020-12-09 09:54:15
>>>2020-12-09 09:54:16
>>>2020-12-09 09:54:17
>>>2020-12-09 09:54:18
>>>2020-12-09 09:54:19
结论
使用该方式来实现定时任务的方式,可以使用指定程序在什么时间来执行数据之后来执该任务。这种如果确定具体使用某个时间来执行,使用该方式较佳。