使用java自带的定时器执行定时任务

使用java自带的定时器执行定时任务

我这里以输出当前系统时间为例:

import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
   * java自带的定時器
 * @author zyl
 * @date 2018年11月11日
 */
@Component
@EnableScheduling //启用定时任务
public class TimeTask {
    // 定時器注解,每一秒执行一次
    @Scheduled(cron = "0/1 * * * * *")
    public void timer() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
        System.out.println("当前系统时间是:" + df.format(new Date()));
    }
}
发布了25 篇原创文章 · 获赞 58 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/student_zz/article/details/93331111