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日
最近浏览更多
sjwc88  LV4 2024年12月13日
Oxygeni  LV7 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日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友