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;
}
}
}
基于时间段的Timer定时器执行任务
最新推荐文章于 2022-12-13 14:15:05 发布