我这里以输出当前系统时间为例:
- 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()));
- }
- }
-