posts - 25,comments - 2,views - 27084

创建定时器对象:

Timer timer = new Timer();

定时执行任务:
只执行一次:
timer.schedule(TimerTask task, long delay)  // 延迟 delay(毫秒)后执行一次 task ;
timer.schedule(TimerTask task, Date time)  // 指定时间 time 执行一次 task;

重复执行:
timer.schedule(TimerTask task, long delay, long period)  // 延迟 delay(毫秒)后首次执行 task,间隔
period 重复执行 task;
timer.
schedule(TimerTask task, Date firstTime, long period)  //指定 firstTime 首次执行 task, 间隔 period 重复执行 task;

例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
    * 指定定时器延迟 delay 后执行任务(只执行一次)
    */
   public static void timer1(){
       Timer timer = new Timer();
       timer.schedule(new TimerTask() {
           @Override
           public void run() {
               System.out.println("当前时间: " + DateUtils.dateToStr(new Date(), DateUtils.sdf_str1));
           }
       }, 1000);
   }
 
   /**
    * 指定某个时间执行定时器(之执行一次),对于一过去的时间立即执行
    */
   public static void timer2(){
       new Timer().schedule(new TimerTask() {
           @Override
           public void run() {
               System.out.println("当前时是: " + DateUtils.dateToStr(new Date(), DateUtils.sdf_str1));
           }
       }, DateUtils.strToDate("2022-03-13 09:54:20"));
   }
 
   /**
    * 延迟 delay 时间后执行,间隔 period 重复执行
    */
   public static void timer3(){
       new Timer().schedule(new TimerTask() {
           @Override
           public void run() {
               System.out.println("当前时间是: " + DateUtils.dateToStr(new Date(), DateUtils.sdf_str1));
           }
       },0, 1000);
   }
 
   /**
    * 设置首次执行时间,间隔 period 重复执行(首次时间已经过去立即执行)
    */
   public static void timer4(){
       new Timer().schedule(new TimerTask() {
           @Override
           public void run() {
               System.out.println("当前时间是: " + DateUtils.dateToStr(new Date(), DateUtils.sdf_str1));
           }
       }, DateUtils.strToDate("2022-05-13 10:10:30"), 1000);
   }

 

时间工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class DateUtils {
    public static String sdf_str1 = "yyyy-MM-dd HH:mm:ss";
 
    public static String sdf_str2 = "yyyy-MM-dd";
 
    public static String sdf_str3 = "HH:mm:ss";
 
 
    private static SimpleDateFormat SDF = new SimpleDateFormat(sdf_str1);
 
    /**
     * 自定义时间转换成字符串
     * @param date
     * @param formate
     * @return
     */
    public static String dateToStr(Date date, String formate){
        SimpleDateFormat sdf = new SimpleDateFormat(formate);
         return sdf.format(date);
    }
 
    public static Date strToDate(String str){
        try {
            return SDF.parse(str);
        } catch (ParseException e) {
//            e.printStackTrace();
            System.err.println("时间转换失败: " + e.getMessage());
        }
        return null;
    }
}

  

 

 

 
posted on   大渔33  阅读(381)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示