请假时间计算(未完成节假日部分)

DateUtils 在另一篇文章内 

package com.thinkgem.jeesite.modules.kaoqin.utils;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.thinkgem.jeesite.common.utils.DateUtils;

/**
 * 请假时间 (跨年、跨月、跨天、节假日)
 * @author xl
 * @see
 */
public class LeaveUtils {

	private static final String GOMTIME = " 08:00:00";
	private static final String OFFMTIME = " 12:00:00";
	private static final String GOATIME = " 13:00:00";
	private static final String OFFATIME = " 17:00:00";
	private static final long HOUR = 1000 * 60 * 60;
	private static final long MINUTE = 1000 * 60;
	
	/**
	 * 
	 * @param sb 
	 * @param dayTime 每天上班时间
	 * @param key  map的key
	 * @param date 请假日期
	 * @return
	 */
	public static Map convertTime(String key,String dayTime,Date date) {
		Map map = new HashMap();
		String day = DateUtils.dateToString("yyyy-MM-dd", date);
		if ((dayTime != null && !"".equals(dayTime))&&
				(key != null && !"".equals(key))) {
			StringBuffer sb = new StringBuffer();
			sb.append(day).append(dayTime);
			map.put(key, sb.toString());
			return map ;
		}
		
		StringBuffer gmtSB = new StringBuffer();
		StringBuffer omtSB = new StringBuffer();
		StringBuffer gatSB = new StringBuffer();
		StringBuffer oatSB = new StringBuffer();
		
		map.put("gmt", gmtSB.append(day).append(GOMTIME).toString());
		map.put("omt", omtSB.append(day).append(OFFMTIME).toString());
		map.put("gat", gatSB.append(day).append(GOATIME).toString());
		map.put("oat", oatSB.append(day).append(OFFATIME).toString());
		return map;
	}
	
	static long b1 = 0l ; 
	static long b2 = 0l ;
	static long b3 = 0l ;
	static long b4 = 0l ;
	static long e1 = 0l ;
	static long e2 = 0l ;
	static long e3 = 0l ;
	static long e4 = 0l ;
	
	/**
	 * dataMap
	 * @param begin
	 * @param end
	 */
	public static void setDataMap(Date begin,Date end){
		Map beginMap = convertTime(null,null,begin);
		b1 = DateUtils.strToDate(null, beginMap.get("omt")).getTime();//12
		b2 = DateUtils.strToDate(null, beginMap.get("gat")).getTime();//1
		b3 = DateUtils.strToDate(null, beginMap.get("oat")).getTime();//5
		Map endMap = convertTime( null, null, end);
		e2 = DateUtils.strToDate(null, endMap.get("omt")).getTime();//12
		e1 = DateUtils.strToDate(null, endMap.get("gat")).getTime();//1
		e4 = DateUtils.strToDate(null, endMap.get("gmt")).getTime();//8
	}
	
	/**
	 * 请假时间总和主要调用
	 * @param begin
	 * @param end
	 * @param type
	 * @return
	 * @throws ParseException
	 */
	public static int sumTime(Date begin,Date end) throws ParseException {
		int count = DateUtils.getWeekdayCount(begin,end,false);
		if (DateUtils.getYear(begin).equals(DateUtils.getYear(end))
				&& DateUtils.getMonth(begin).equals(DateUtils.getMonth(begin))) {//同年同月	
			setDataMap(begin,end);
			if (DateUtils.getDay(begin).equals(DateUtils.getDay(end))) {				
				return matchLeaveDays(begin, end, 0) - 8 * count;
			}else {
				return matchLeaveDays(begin, end, 1) - 8 * count;
			}
		}else{ //跨年或跨月		
			return matchLeaveDays(begin, end, 2) - 8 * count;
		}		
	} 
	
	/**
	 * 计算请假时间
	 * @param begin
	 * @param end
	 * @param type
	 * @return
	 */
	private static int matchLeaveDays(Date begin,Date end,int type){
		if (type == 0) {//不跨天
			if (begin.getTime()e1) {
				int leaveTime = (int)((end.getTime() - begin.getTime())/HOUR);
				return leaveTime-1;
			}
			int leaveTime = (int)((end.getTime() - begin.getTime())/HOUR);
			return leaveTime;
		}else if (type==1){//跨天
			return leave(begin,end);
		}else if(type == 2){ //跨年跨月
			Date beginEnd = DateUtils.getMonthLastDay(begin);
			setDataMap(begin,beginEnd);
			leave(begin,beginEnd);
			Date endBegin = DateUtils.getMonthFirstDay(end);
			setDataMap(endBegin,end);
			leave(endBegin,end);
		}
		return type;
	}
	
	/**
	 * 计算请假时间 
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int leave(Date begin,Date end){
		int intervalDay=Integer.valueOf(DateUtils.getDay(end)) - 
				Integer.valueOf(DateUtils.getDay(begin));
		int leaveTime1 = 0;
		int leaveTime2 = 0;
		if ((begin.getTime()e1)) {//下午-下午
			leaveTime1 = (int)((b3 - begin.getTime())/HOUR) -1;
			leaveTime2 = (int)((end.getTime() -e4)/HOUR);
		}else if ((begin.getTime()e1)) {//上午-下午
			leaveTime1 = (int)((b3 - begin.getTime())/HOUR) -1;
			leaveTime2 = (int)((end.getTime() - e4)/HOUR)-1;
		}else if(b2

 

你可能感兴趣的:(请假时间计算(未完成节假日部分))