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");
}
}
Java 每天定时执行的Timer
最新推荐文章于 2022-10-25 18:38:24 发布