|
| 1 | +package org.javacore.scheduler; /* |
| 2 | + * Copyright [2015] [Jeff Lee] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import java.util.Calendar; |
| 18 | +import java.util.Date; |
| 19 | +import java.util.concurrent.Executors; |
| 20 | +import java.util.concurrent.ScheduledExecutorService; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +/** |
| 24 | + * ScheduledExecutorService的使用 |
| 25 | + * @author BYSocket |
| 26 | + * @since 2016-01-10 22:02:00 |
| 27 | + */ |
| 28 | +public class SchedulerExecutorTest2 implements Runnable{ |
| 29 | + |
| 30 | + private final String jobName; |
| 31 | + |
| 32 | + public SchedulerExecutorTest2(String jobName) { |
| 33 | + this.jobName = jobName; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void run() { |
| 38 | + System.out.println("Date = " + new Date() + ", running => " + jobName); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * 计算从当前时间currentDate开始,满足条件dayOfWeek, hourOfDay, |
| 43 | + * minuteOfHour, secondOfMinite的最近时间 |
| 44 | + * @return |
| 45 | + */ |
| 46 | + public Calendar getEarliestDate(Calendar currentDate, int dayOfWeek, |
| 47 | + int hourOfDay, int minuteOfHour, int secondOfMinite) { |
| 48 | + //计算当前时间的WEEK_OF_YEAR,DAY_OF_WEEK, HOUR_OF_DAY, MINUTE,SECOND等各个字段值 |
| 49 | + int currentWeekOfYear = currentDate.get(Calendar.WEEK_OF_YEAR); |
| 50 | + int currentDayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK); |
| 51 | + int currentHour = currentDate.get(Calendar.HOUR_OF_DAY); |
| 52 | + int currentMinute = currentDate.get(Calendar.MINUTE); |
| 53 | + int currentSecond = currentDate.get(Calendar.SECOND); |
| 54 | + |
| 55 | + //如果输入条件中的dayOfWeek小于当前日期的dayOfWeek,则WEEK_OF_YEAR需要推迟一周 |
| 56 | + boolean weekLater = false; |
| 57 | + if (dayOfWeek < currentDayOfWeek) { |
| 58 | + weekLater = true; |
| 59 | + } else if (dayOfWeek == currentDayOfWeek) { |
| 60 | + //当输入条件与当前日期的dayOfWeek相等时,如果输入条件中的 |
| 61 | + //hourOfDay小于当前日期的 |
| 62 | + //currentHour,则WEEK_OF_YEAR需要推迟一周 |
| 63 | + if (hourOfDay < currentHour) { |
| 64 | + weekLater = true; |
| 65 | + } else if (hourOfDay == currentHour) { |
| 66 | + //当输入条件与当前日期的dayOfWeek, hourOfDay相等时, |
| 67 | + //如果输入条件中的minuteOfHour小于当前日期的 |
| 68 | + //currentMinute,则WEEK_OF_YEAR需要推迟一周 |
| 69 | + if (minuteOfHour < currentMinute) { |
| 70 | + weekLater = true; |
| 71 | + } else if (minuteOfHour == currentSecond) { |
| 72 | + //当输入条件与当前日期的dayOfWeek, hourOfDay, |
| 73 | + //minuteOfHour相等时,如果输入条件中的 |
| 74 | + //secondOfMinite小于当前日期的currentSecond, |
| 75 | + //则WEEK_OF_YEAR需要推迟一周 |
| 76 | + if (secondOfMinite < currentSecond) { |
| 77 | + weekLater = true; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + if (weekLater) { |
| 83 | + //设置当前日期中的WEEK_OF_YEAR为当前周推迟一周 |
| 84 | + currentDate.set(Calendar.WEEK_OF_YEAR, currentWeekOfYear + 1); |
| 85 | + } |
| 86 | + // 设置当前日期中的DAY_OF_WEEK,HOUR_OF_DAY,MINUTE,SECOND为输入条件中的值。 |
| 87 | + currentDate.set(Calendar.DAY_OF_WEEK, dayOfWeek); |
| 88 | + currentDate.set(Calendar.HOUR_OF_DAY, hourOfDay); |
| 89 | + currentDate.set(Calendar.MINUTE, minuteOfHour); |
| 90 | + currentDate.set(Calendar.SECOND, secondOfMinite); |
| 91 | + return currentDate; |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + public static void main(String[] args) { |
| 96 | + SchedulerExecutorTest2 executor = new SchedulerExecutorTest2("job1"); |
| 97 | + // 获取当前时间 |
| 98 | + Calendar currentDate = Calendar.getInstance(); |
| 99 | + long currentDateLong = currentDate.getTime().getTime(); |
| 100 | + System.out.println("Current Date = " + currentDate.getTime().toString()); |
| 101 | + // 计算满足条件的最近一次执行时间 |
| 102 | + Calendar earliestDate = executor.getEarliestDate(currentDate,3,16,38,10); |
| 103 | + long earliestDateLong = earliestDate.getTime().getTime(); |
| 104 | + System.out.println("Earliest Date = " + earliestDate.getTime().toString()); |
| 105 | + // 计算从当前时间到最近一次执行时间的时间间隔 |
| 106 | + long delay = earliestDateLong - currentDateLong; |
| 107 | + // 计算执行周期为一星期 |
| 108 | +// long period = 7 * 24 * 60 * 60 * 1000; |
| 109 | + long period = 1000; |
| 110 | + ScheduledExecutorService service = Executors.newScheduledThreadPool(10); |
| 111 | + // 从现在开始delay毫秒之后,每隔一星期执行一次job1 |
| 112 | + service.scheduleAtFixedRate(executor, delay, period, |
| 113 | + TimeUnit.MILLISECONDS); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments