基于时间段的Timer定时器执行任务

package com.timerTools;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TestTimer {  
	Timer timer = new Timer();  

    //定时器,执行任务测试
    public static void main(String[] args) {  
    	new TestTimer().executeTask(3,null,1000,1000); 
    } 
    
	/**
	 * 定时器任务制定,执行
	 * @param tag: 0.在指定的时间里,执行指定的任务;
	 * 			   1.在指定的时间里,按照指定的延迟,重复执行指定任务;
	 *             2.从指定的延迟后,执行指定任务;
	 *             3.从指定的延迟后,按照指定的延迟,重复执行指定的任务。
	 *             
	 * @param mydate:指定的执行时间。只有在tag等于0、1才需要指定。            
	 * @param delay: 延迟执行时间,毫秒数。只有在tag等于1、3才需要指定。
	 * @param period:间隔执行时间,毫秒数。只有在tag等于4才需要指定。
	 */
    public void executeTask(int tag, Date mydate,long delay, long period) {  
    	if(tag == 0){
    		timer.schedule(new DefineTimerTask(), mydate);
    	}else if(tag == 1){
    		timer.schedule(new DefineTimerTask(), mydate, period);
    	}else if(tag == 2){
    		timer.schedule(new DefineTimerTask(),delay);
    	}else if(tag == 3){
    		timer.schedule(new DefineTimerTask(),delay,period);
    	}else{
    		System.out.println("Error:输入的定时器参数无效!");
    	}  
    } 
    
	/** 
	 * 执行的定时器任务
	 */  
    private class DefineTimerTask extends TimerTask {  
        //执行的次数  
    	boolean isTimes = true;  
        
        public void run() {  
        	isTimes = isTimes();
        	if(isTimes){
        		System.out.println("执行定时器任务开始 :" + System.currentTimeMillis());  
        	}else{
        		System.out.println("----------------- 执行定时器任务结束:" + System.currentTimeMillis());  
        		timer.cancel(); 
        	}
        }  
    }  
    
    
    
    
    //是否满足时间周期
    public static boolean isTimes(){
    	boolean isTimes = false;
    	String newWeek = "2";
    	String times = "17:00-17:10,17:10-17:15,17:24-17:25,17:25-17:26"; //
    	
    	String[] newWeeks = newWeek.split(",");
    	String[] timess   =  times.split(",");

    	Calendar   cal   =   Calendar.getInstance(); 
    	cal.setTime(new Date()); 
    	
    	int week = cal.get(Calendar.DAY_OF_WEEK);
    	if(week == 1){
    		week = 7;
    	}else{
    		week = week-1;
    	}
    	
        try {
        	for(int i=0;i<newWeeks.length;i++){
        		//是否是指定的周期
        		if(Integer.parseInt(newWeeks[i]) == week){
        			for(int j=0;j<timess.length;j++){
        				//是否是指定的时间段
        				if(isInDates(timess[j])){
        					System.out.println("----------------- 执行定时器任务时间:[" +timess[j]+"]");  
        					return true;
        				}
        			}
        		}
        	}
		} catch (Exception e) {}
		
		return isTimes;
    }
    
    //比较两个时间是否存在交集
     public static boolean isInDates(String times){   
        Date dt = new Date();
        int strDateH = dt.getHours();  
        int strDateM = dt.getMinutes();  
          
        int strDateBeginH = Integer.parseInt(times.split("-")[0].split(":")[0]);  
        int strDateBeginM = Integer.parseInt(times.split("-")[0].split(":")[1]);  
          
        int strDateEndH = Integer.parseInt(times.split("-")[1].split(":")[0]);  
        int strDateEndM = Integer.parseInt(times.split("-")[1].split(":")[1]);  
        
        if((strDateH>=strDateBeginH && strDateH<=strDateEndH)){  
        	if(strDateH>strDateBeginH && strDateH<strDateEndH){  
                return true;  
            }else if(strDateH==strDateBeginH && strDateM>strDateBeginM && strDateH<strDateEndH){  
                return true;  
            }else if(strDateH==strDateBeginH && strDateM==strDateBeginM && strDateH<strDateEndH){  
                return true;  
            }else if(strDateH==strDateBeginH && strDateM==strDateBeginM && strDateH<strDateEndH){  
                return true;  
            }else if(strDateH==strDateBeginH && strDateH==strDateEndH && strDateM<strDateEndM){  
                return true;  
            }else if(strDateH>strDateBeginH && strDateH==strDateEndH && strDateM<strDateEndM){  
                return true;  
            }else if(strDateH>strDateBeginH && strDateH==strDateEndH && strDateM==strDateEndM ){  
                return true;  
            }else if(strDateH>strDateBeginH && strDateH==strDateEndH && strDateM==strDateEndM){  
                return true;  
            }else{  
                return false;  
            }  
        }else{  
            return false;  
        }  
    }  
    
}  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: timer定时器是一种计时工具,常用于需要定时执行某些任务的场景中。它能够在设定的时间到达后触发某个事件或执行某段代码。 timer定时器的使用非常灵活,可以根据需求设置不同的时间间隔和执行次数。常见的定时器有单次定时器和循环定时器两种。 单次定时器可以在指定的时间间隔后执行一次任务,并且只执行一次。例如,我们可以设置一个单次定时器,在5秒后触发某个函数执行。 循环定时器可以按照设定的时间间隔不断重复执行任务。例如,我们可以设置一个循环定时器,每隔1分钟触发某个函数执行timer定时器不仅可以用于编程语言中,也可以用在各种电子设备中。比如,我们可以在手机上设置一个定时器,用来提醒我们在某个时间做某件事情,或者在烹饪过程中使用定时器来控制食物的烹饪时间。 总之,timer定时器是一种非常实用的工具,可以在需要定时执行任务的场景中发挥重要作用。无论是在编程中还是在日常生活中,我们都可以利用timer定时器来提高效率和方便操作。 ### 回答2: timer定时器是一种用于控制时间间隔的设备或工具。它可以在给定的时间间隔内执行特定的任务或操作。 timer定时器通常用于各种计时和定时操作,例如在游戏中实现角色的移动、动画效果的切换,或者定时播放音乐等。 在编程中,我们可以使用timer定时器来实现事件的定时触发。通过设置一个特定的时间间隔和一个回调函数,当时间到达设定的时间间隔时,定时器将会触发回调函数,并执行其中的代码。 timer定时器可以在编程语言中以不同形式实现,例如在C语言中,我们可以使用标准库中的函数来创建和使用定时器。在C语言中,我们通常需要定义一个回调函数,用于实现定时触发时需要执行的操作,然后使用定时器函数来设置时间间隔和回调函数。 定时器的使用可以帮助我们在程序中实现各种时间相关的任务,提高程序的可靠性和运行效率。但是需要注意的是,在使用定时器时需要合理设置时间间隔,避免频繁触发定时器导致系统负荷过大和性能下降。 总结起来,timer定时器是一种用于控制时间间隔的设备或工具,它在编程中可以用来实现各种时间相关的操作。通过设置时间间隔和回调函数,我们可以实现定时触发并执行相应的代码。 ### 回答3: timer定时器是一种用来测量或控制时间间隔的设备或工具。它通常用于在特定时间间隔内执行一些操作或发出信号。 timer定时器有广泛的应用领域。在家庭生活中,我们可以使用定时器来设置闹钟,提醒我们起床、上班、做饭等。在厨房中,我们可以设置烤箱定时器来保证食物在正确定时烹饪,防止过度或不足。 在工业领域,timer定时器被广泛应用于自动化系统中。它可以用来控制设备的运行时间,例如定时关闭空调或照明设备,节约能源。定时器还可以用于控制机器的运转周期,在特定时间间隔内启动或停止机器,提高生产效率。 在计算机编程中,timer定时器也是一个重要的概念。它可以用来控制代码的执行时间,例如设置一个定时器在特定时间间隔内反复执行某个函数或代码块。这在游戏开发中很常见,用来实现动画效果或定期更新游戏状态。 总之,timer定时器在我们的生活和工作中扮演着重要的角色。无论是作为一个简单的时间管理工具还是一个复杂的控制设备,它都能够帮助我们提高效率、节约时间,并实现各种不同的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值