天天看点

请假系统特例规则详细设计

一、查看设计的思维导图

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test 
{
    public static String getTimeCoincidence(Date beginTime1, Date endTime1, Date beginTime2, Date endTime2) 
    {
        long b1 = beginTime1.getTime();
        long e1 = endTime1.getTime();
        long b2 = beginTime2.getTime();
        long e2 = endTime2.getTime();

        String coincidenceTime;

        if (b1 <= b2 && e1 >= e2) 
        {
            coincidenceTime = getTimeDifference(endTime2, beginTime2);
        } else if (b1 >= b2 && e1 <= e2) 
        {
            coincidenceTime = getTimeDifference(endTime1, beginTime1);
        } else if (b1 >= b2 && b1 <= e2 && e2 <= e1) 
        {
            coincidenceTime = getTimeDifference(endTime2, beginTime1);
        } else if (b1 <= b2 && e1 <= e2 && e1 >= b2) 
        {
            coincidenceTime = getTimeDifference(endTime1, beginTime2);
        } else if (e1 <= b2 || b1 >= e2) 
        {
            coincidenceTime = "0";
        } else 
        {
            coincidenceTime = "";
            System.out.println("意料外的日期组合,无法计算重合!");
        }
        return coincidenceTime;
    }

    /**
     * 计算两个时间差
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static String getTimeDifference(Date date1, Date date2) {
        StringBuffer df = new StringBuffer();

        try {
            // 计算单位设置(毫秒1000 * 秒60 * 分60 * 时24);
            long num = (date1.getTime() - date2.getTime()) / (1000 * 60);
            if (num >= 0)
                df.append(num);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return df.toString();
    }

    /**
     * 字符串转时间格式
     * 
     * @param date
     * @return
     */
    public static Date stringToDate(String date) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = format.parse(date, pos);
        return strtodate;

    }

    public static void main(String[] args) {
        Date shangwuStart = stringToDate("2013-01-01 08:00:00");
        Date shangwuStop = stringToDate("2013-01-01 11:30:00");
        
        Date xiawuStart = stringToDate("2013-01-01 13:30:00");
        Date  xiawuStop = stringToDate("2013-01-01 17:30:00");
        
        Date beginTime2 = stringToDate("2013-01-01 09:30:00");
        Date endTime2 = stringToDate("2013-01-01 15:20:00");

        System.out.println("上午请假:"+getTimeCoincidence(shangwuStart, shangwuStop, beginTime2, endTime2)+"分钟");
        System.out.println("下午请假:"+getTimeCoincidence(xiawuStart, xiawuStop, beginTime2, endTime2)+"分钟");
    }
}      
上午请假:120分钟
下午请假:110分钟