SSM:
在@service中写以下方法,具体时间参数的设定,可以参照以下文章
https://www.cnblogs.com/2016-10-10/p/6283321.html
@Scheduled(cron = "0/5 * * * * ?") public void testwork() { Calendar date=Calendar.getInstance(); System.err.println(date.getTime()+"毫秒为: "+System.currentTimeMillis()); MyBlog.setOld_day(date.get(Calendar.DAY_OF_MONTH)); MyBlog.setOld_month(date.get(Calendar.MONTH)+1); MyBlog.setYou_day(date.get(Calendar.DAY_OF_MONTH)); MyBlog.setYou_month(date.get(Calendar.MONTH)+1); System.out.println(MyBlog.getOld_day()); System.out.println(MyBlog.getOld_month()); System.out.println(MyBlog.getYou_day()); System.out.println(MyBlog.getYou_month()); }
并且需要在application.xml中配置以下属性
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
同时开启定时任务即可
<task:annotation-driven/>
SpringBoot
首先需要在主类上加上定时任务注解
@EnableScheduling // 开启定时任务
之后在@Service类中写定时方法即可
/*
* second minute, hour, day of month, month,week
* and day of week. e.g. {@code "0 * * * * MON-FRI"} means once per minute on
* */
// @Scheduled(cron="0 * * * * MON-FRI")
// public void schedul(){
// // 这个方法就可以实现每天定时检查数据,若发现不对的地方即可调用方法通知管理员。
// System.out.println("这是一个定时方法~");
//
// }