天天看點

請假系統特例規則詳細設計

一、檢視設計的思維導圖

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分鐘