package com.ruoyi.web.utils;

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

public class DateRangeUtils {
    /**
     * 校验时间是否在指定范围内
     * @param checkDate
     * @return
     * @throws ParseException
     */
    public  static Boolean checkDateRange(String checkDate, String dataBeginAm, String dataEndAm, String dataBeginPm, String dataEndPm) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date today = sdf.parse(checkDate);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");//设置日期格式

        Date nowTime =df.parse(df.format(today));
        //上午的规定时间
        Date amBeginTime = df.parse(dataBeginAm);
        Date amEndTime = df.parse(dataEndAm);
        //下午的规定时间
        Date pmBeginTime = df.parse(dataBeginPm);
        Date pmEndTime = df.parse(dataEndPm);

        //调用判断方法是否在规定时间段内
        boolean isTime = timeCalendar(nowTime, amBeginTime, amEndTime,pmBeginTime,pmEndTime);

        if(isTime){
            //处于规定的时间段内
            System.out.println(isTime);
        }else{
            //不处于规定的时间段内
            System.out.println(isTime);
        }
        return isTime;
    }

    public static boolean timeCalendar(Date nowTime, Date amBeginTime, Date amEndTime, Date pmBeginTime, Date pmEndTime) {
        //设置当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        //设置开始时间
        Calendar amBegin = Calendar.getInstance();
        amBegin.setTime(amBeginTime);//上午开始时间
        Calendar pmBegin = Calendar.getInstance();
        pmBegin.setTime(pmBeginTime);//下午开始时间
        //设置结束时间
        Calendar amEnd = Calendar.getInstance();
        amEnd.setTime(amEndTime);//上午结束时间
        Calendar pmEnd = Calendar.getInstance();
        pmEnd.setTime(pmEndTime);//下午结束时间
        //处于开始时间之后，和结束时间之前的判断
        if ((date.after(amBegin) && date.before(amEnd)) || (date.after(pmBegin) && date.before(pmEnd))) {
            return true;
        } else {
            return false;
        }
    }
}
