首页>代码>java中任务调度java.util.Timer,ScheduledExecutor,Quartz的机制说明和demo代码实例分享>/testScheduler/src/com/zuidaima/ScheduledExceutorTest2.java
package com.zuidaima; import java.util.Calendar; import java.util.Date; import java.util.TimerTask; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** * @author Lvoy * 使用 ScheduledExcetuor和 Calendar进行任务调度 */ public class ScheduledExceutorTest2 extends TimerTask{ private String jobName = ""; public ScheduledExceutorTest2(String jobName) { super(); this.jobName = jobName; } @Override public void run() { System.out.println("Date = "+new Date()+", execute " + jobName); } /** * 计算从当前时间currentDate开始,满足条件dayOfWeek, hourOfDay, * minuteOfHour, secondOfMinite的最近时间 * @return */ public Calendar getEarliestDate(Calendar currentDate, int dayOfWeek, int hourOfDay, int minuteOfHour, int secondOfMinite) { //计算当前时间的WEEK_OF_YEAR,DAY_OF_WEEK, HOUR_OF_DAY, MINUTE,SECOND等各个字段值 int currentWeekOfYear = currentDate.get(Calendar.WEEK_OF_YEAR); int currentDayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK); int currentHour = currentDate.get(Calendar.HOUR_OF_DAY); int currentMinute = currentDate.get(Calendar.MINUTE); int currentSecond = currentDate.get(Calendar.SECOND); //如果输入条件中的dayOfWeek小于当前日期的dayOfWeek,则WEEK_OF_YEAR需要推迟一周 boolean weekLater = false; if (dayOfWeek < currentDayOfWeek) { weekLater = true; } else if (dayOfWeek == currentDayOfWeek) { //当输入条件与当前日期的dayOfWeek相等时,推迟一周推迟一周 if (hourOfDay < currentHour) { //hourOfDay小于当前日期 需要推迟一周 weekLater = true; } else if (hourOfDay == currentHour) { //当输入条件与当前日期的dayOfWeek, hourOfDay相等时, 需要推迟一周 if (minuteOfHour < currentMinute) { //如果输入条件中的minuteOfHour小于当前日期的 weekLater = true; } else if (minuteOfHour == currentSecond) { //当输入条件与当前日期的dayOfWeek, hourOfDay, 需要推迟一周 if (secondOfMinite < currentSecond) { //secondOfMinite小于当前日期的currentSecond,需要推迟一周 weekLater = true; } } } } if (weekLater) { //设置当前日期中的WEEK_OF_YEAR为当前周推迟一周 currentDate.set(Calendar.WEEK_OF_YEAR, currentWeekOfYear + 1); } // 设置当前日期中DAY_OF_WEEK,HOUR_OF_DAY,MINUTE,SECOND的值 currentDate.set(Calendar.DAY_OF_WEEK, dayOfWeek); currentDate.set(Calendar.HOUR_OF_DAY, hourOfDay); currentDate.set(Calendar.MINUTE, minuteOfHour); currentDate.set(Calendar.SECOND, secondOfMinite); return currentDate; } public static void main(String[] args) throws Exception { ScheduledExceutorTest2 test = new ScheduledExceutorTest2("zuidaima1"); //获取当前时间 Calendar currentDate = Calendar.getInstance(); long currentDateLong = currentDate.getTime().getTime(); System.out.println("当前时间 = " + currentDate.getTime().toString()); //计算满足条件的最近一次执行时间 Calendar earliestDate = test.getEarliestDate(currentDate, 5, 17, 20, 10); long earliestDateLong = earliestDate.getTime().getTime(); System.out.println("下一次执行时间 = "+ earliestDate.getTime().toString()); //计算从当前时间到最近一次执行时间的时间差 long delay = earliestDateLong - currentDateLong; //计算执行周期为一星期 long period = 7 * 24 * 60 * 60 * 1000; ScheduledExecutorService service = Executors.newScheduledThreadPool(10); //从现在开始delay毫秒之后,每隔一星期执行一次zuidaima1 service.scheduleAtFixedRate(test, delay, period,TimeUnit.MILLISECONDS); } }
最近下载更多
123456qqwqa LV11
2019年7月16日
kk268137 LV9
2019年3月25日
四季镇 LV9
2018年9月4日
元芳你怎么看 LV15
2018年5月29日
wwwdawn LV14
2018年5月23日
gaoshuangwen LV2
2017年12月20日
wang1991987 LV2
2017年11月23日
xiangge LV1
2017年10月17日
hujialuo LV3
2017年8月28日
18202838208 LV14
2017年8月7日
最近浏览更多
Oxygeni LV4
2023年4月29日
cx330a LV2
2023年4月26日
林间听风 LV10
2022年8月16日
随缘而遇 LV2
2021年6月16日
sangyy LV10
2020年11月5日
trstrs LV1
2020年10月22日
jackychen1012 LV2
2020年9月23日
kenhomeliu LV29
2020年6月19日
912299793 LV21
2020年5月8日
likezhensan LV8
2020年4月25日