Java 每天定时执行的Timer

本文介绍了一个自定义的Java定时任务类DayTimer,该类继承了TimerTask并实现了按天重复执行的功能。支持节假日排除功能,并提供了灵活的时间配置选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.etnet.ui;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;

public class DayTimer extends TimerTask {

	private Timer timer;
	private Runnable runnable;
	private final long PERIOD = 24 * 60 * 60 * 1000;
	private boolean isNeedExcute = true;
	private SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
	private SimpleDateFormat format2 = new SimpleDateFormat("MMdd");
	private Calendar cld = Calendar.getInstance();
	private boolean isExceptHoliday = false;
	private List<String> festivalList;
	private List<String> specialWorkDayList;

	public DayTimer(Runnable runnable) {
		this(runnable, false);
	}

	public DayTimer(Runnable runnable, boolean isExceptHoliday) {
		this.runnable = runnable;
		this.isExceptHoliday = isExceptHoliday;
		timer = new Timer();
	}

	public void start(String everyDayExcuteTime) {
		Date firstTime = null;
		try {
			firstTime = parseDate(everyDayExcuteTime);
		} catch (ParseException e) {
			e.printStackTrace();
			return;
		}
		Date currDate = new Date();
		if (currDate.after(firstTime)) {
			isNeedExcute = false;
		}
		if (this.runnable != null) {
			this.timer.scheduleAtFixedRate(this, firstTime, PERIOD);
		}
	}

	public void stop() {
		this.timer.cancel();
	}

	private Date parseDate(String everyDayExcuteTime) throws ParseException {
		Pattern pattern = Pattern.compile("\\d\\d:\\d\\d$");
		if (!pattern.matcher(everyDayExcuteTime).matches()) {
			throw new ParseException("time String format is HH:mm", 0);
		}
		String currYearMonthday = format.format(cld.getTime());
		String timeStr = currYearMonthday + everyDayExcuteTime;
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyyMMddHH:mm").parse(timeStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	@Override
	public final void run() {
		if (this.runnable != null) {
			if (isExceptHoliday) {
				checkHoliday();
			}
			if (isNeedExcute) {
				this.runnable.run();
			} else {
				isNeedExcute = true;
			}
		}
	}

	private void checkHoliday() {
		cld = Calendar.getInstance();
		String todayMouthDayStr = this.format2.format(cld.getTime());
		int week = cld.get(Calendar.DAY_OF_WEEK);
		if (this.festivalList != null && this.festivalList.contains(todayMouthDayStr)) {
			isNeedExcute = false;
		} else if (this.specialWorkDayList != null
				&& !this.specialWorkDayList.contains(todayMouthDayStr)
				&& (week == Calendar.SATURDAY || week == Calendar.SUNDAY)) {
			isNeedExcute = false;
		} else if (week == Calendar.SATURDAY || week == Calendar.SUNDAY) {
			isNeedExcute = false;
		}
	}

	// day format need to be MMdd in list. e.g. 0501(Labor Day)
	public void setFestivalList(List<String> festivalList) {
		this.festivalList = festivalList;
	}

	// day format need to be MMdd in list e.g. 1001(National Day)
	public void setSpecailWorkDayList(List<String> specialWorkDayList) {
		this.specialWorkDayList = specialWorkDayList;
	}

	public static void main(String[] args) {
		new DayTimer(new Runnable() {

			@Override
			public void run() {
				System.out.println("execute a time");

			}

		}).start("11:28");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值