package net.northking.cctp.dw.utils;

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

public class DateDealUtils {

    /**
     * 计算两日期之间的工作日
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int calculateWorkDay(Date startDate, Date endDate) {
        int result = 0;
        Date sd = startDate;
        Calendar c1 = Calendar.getInstance();
        c1.setTime(sd);
        while (!sd.after(endDate)) {
            int i = c1.get(Calendar.DAY_OF_WEEK);
            if (i != Calendar.SUNDAY && i != Calendar.SATURDAY) {
                result++;
            }

            c1.add(Calendar.DAY_OF_MONTH, 1);
            sd = c1.getTime();
        }
        return result;
    }

    /**
     * 获取上周六
     * 若今天是周六，则返回今天
     *
     * @return
     */
    public static Date getLastSat() {
        Calendar calendar = Calendar.getInstance();
        if (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
            calendar.add(Calendar.WEEK_OF_YEAR, -1);
        }
        return calendar.getTime();
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getCurrentDate() {
        Date currentDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(currentDate);
    }

}
