用到的组件:
- springboot的实现CommandLineRunner接口或者使用@PostContruct注解
- Timer、TimerTask
- LocalDateTime
具体实现:
- 一、实现CommandLineRunner
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
[@Component](https://my.oschina.net/u/3907912)
public class MyCommandLineRunner implements CommandLineRunner {
[@Override](https://my.oschina.net/u/1162528)
public void run(String... var1) throws Exception{
System.out.println("This will be execute when the project was started!");
new DemoController().demo();
}
}
- DemoController
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* [@Author](https://my.oschina.net/arthor) he qing yu
* [@Date](https://my.oschina.net/u/2504391) 2021/4/19 10:33
* @Version 1.0
*/
public class DemoController {
// 指定触发的时间
LocalDateTime localDateTime = LocalDateTime.of(2021, 04, 19, 11, 00, 01);
public void demo(){
/**
* localDateTime.get(ChronoField.YEAR); 获取年
* localDateTime.get(ChronoField.MONTH_OF_YEAR); 获取月
* localDateTime.get(ChronoField.DAY_OF_MONTH); 获取日
* localDateTime.get(ChronoField.HOUR_OF_DAY); 获取小时
* localDateTime.get(ChronoField.MINUTE_OF_HOUR); 获取分
* localDateTime.get(ChronoField.SECOND_OF_MINUTE); 获取秒
*/
System.out.println(
localDateTime.get(ChronoField.YEAR)+":"
+localDateTime.get(ChronoField.MONTH_OF_YEAR)+":"
+localDateTime.get(ChronoField.DAY_OF_MONTH)+":"
+localDateTime.get(ChronoField.HOUR_OF_DAY)+":"
+localDateTime.get(ChronoField.MINUTE_OF_HOUR)+":"
+localDateTime.get(ChronoField.SECOND_OF_MINUTE)
);
Calendar calendar = Calendar.getInstance();
/**
* 指定触发的时间
* */
calendar.set(Calendar.YEAR, localDateTime.get(ChronoField.YEAR)); // 设置年份
calendar.set(Calendar.DAY_OF_MONTH, localDateTime.get(ChronoField.DAY_OF_MONTH));//设置日期
calendar.set(Calendar.MONTH, localDateTime.get(ChronoField.MONTH_OF_YEAR)-1);//设置日期为月份 这里3表示4月份 4就表示5月份
calendar.set(Calendar.HOUR_OF_DAY, localDateTime.get(ChronoField.HOUR_OF_DAY)); //设置触发时
calendar.set(Calendar.MINUTE, localDateTime.get(ChronoField.MINUTE_OF_HOUR)); //设置触发分
calendar.set(Calendar.SECOND, localDateTime.get(ChronoField.SECOND_OF_MINUTE)); //设置触发秒
Date time = calendar.getTime();
Timer timer = new Timer();
timer.schedule(new RemindTask(), time);
}
class RemindTask extends TimerTask {
public void run() {
System.out.println("localDateTime:"+localDateTime);
}
}
}
上面参考: https://blog.csdn.net/xihulu_a/article/details/115860647
二、使用@PostContruct:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.*;
@Component
public class TermUpgradeService {
List<LocalDateTime> localDateTimeList = new ArrayList<>();
{
// 指定触发的时间
LocalDateTime localDateTime = LocalDateTime.of(2022, 07, 18, 18, 40, 30);
localDateTimeList.add(localDateTime);
}
@PostConstruct
public void init(){
new DemoController().demo();
}
}