package com.tofly.watermeter.custom.util;

import com.tofly.common.core.util.Date8Util;

import java.util.Calendar;
import java.util.Date;

/**
 * @author by 汪截流
 * @date 2020/9/2.
 */
public class Date7Utils {
    /**
     * 自定义的方法；根据周期计算下次周检时间
     *
     * @param period 相加月份
     * @return
     */
    public static Date addNextPeriod(int period, Date nowTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowTime);
        calendar.add(Calendar.MONTH, period);//增加月份
        Date time = calendar.getTime();
        return time;
    }

    /**
     * 计算时间的；与上面的是一样的，优化了类型计算
     * 根据传递时间，计算差值，类型；计算出时间
     * @param date 传递时间
     * @param num 差值
     * @param type 类型 如：月份：Calendar.MONTH；小时：Calendar.HOUR_OF_DAY
     * @return
     */
    public static Date dateTimeDifference(Date date,long num,int type){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(type,(int)num);
        return cal.getTime();
    }

    /**
     * 自定义方法：判断当前月属于季度的上中下哪一个阶段
     *
     * @return
     */
    public static String judgeQuarter() {
        //判断属于季度的上中下；如：季度上：1 4 7 10 月   季度中：2 5 8 11 月   季度下：3 6 9 12 月
        //根据以上规律可以发现；季度上除以3余数为 1 ；季度中除以3余数为 2 ； 季度下除以3余数为 0 ；以此可以判断季度的上中下
        int month = Date8Util.getMonth();
        String quarter = month % 3 == 1 ? "季度上" : month % 3 == 2 ? "季度中" : "季度下";
        System.out.println(month + quarter);
        return quarter;
    }

    /**
     * 计算时间相差用时
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static String diffDate(Date startTime, Date endTime) {
        //这样得到的差值是毫秒级别
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = (diff / (60 * 60 * 1000) - day * 24);
        long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        System.out.println(day + "天" + hour + "小时" + min + "分" + s + "秒");

        return day + "天" + hour + "小时" + min + "分" + s + "秒";
    }

    /**
     * 计算时间相差天数
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static Long diffDateDay(Date startTime, Date endTime) {
        //这样得到的差值是毫秒级别
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff / (24 * 60 * 60 * 1000);
        return day;
    }

    public static long diffDateMinute(Date startTime, Date endTime) {
        //这样得到的差值是毫秒级别
        long diff = endTime.getTime() - startTime.getTime();
        long minute = diff / 1000;
        return minute;
    }


    /**
     * 对应时间加秒
     * @param time
     * @param second
     * @return
     */
    public static Date addSecond(Date time, int second) {
        Calendar newTime = Calendar.getInstance();
        newTime.setTime(time);
        //日期加秒
        newTime.add(Calendar.SECOND, second);
        Date dt1 = newTime.getTime();
        return dt1;

    }


}
