package com.zm.order.tool.server.utils;

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

/**
 * @Description 时间工具类
 * @Author: ph
 * @Date: 2019/11/26 13:55
 */
public class DateUtils {

    /**
     * 获取某个时间的指定时间单元后的日期
     */
    public static Date next(int timeUnit, int amount) {
        Calendar currentTime = Calendar.getInstance();
        currentTime.add(timeUnit, amount);
        Date nextTime = currentTime.getTime();
        return nextTime;
    }

    /**
     * 获取某个时间的指定时间单元前的日期
     */
    public static Date before(int timeUnit, int amount) {
        Calendar currentTime = Calendar.getInstance();
        currentTime.add(timeUnit, -amount);
        return currentTime.getTime();
    }

    public static void main(String[] args) {
        System.out.println();
        System.out.println(getEndDate(before(Calendar.DAY_OF_YEAR,1)).getTime());
    }

    /**
     * 获取某个时间的指定时间单元后的日期
     */
    public static Date next(Date date, int timeUnit, int amount) {
        Calendar currentTime = Calendar.getInstance();
        currentTime.setTime(date);
        currentTime.add(timeUnit, amount);
        Date nextTime = currentTime.getTime();
        return nextTime;
    }

    /**
     * 获取某个时间的指定时间单元前的日期
     */
    public static Date before(Date date, int timeUnit, int amount) {
        Calendar currentTime = Calendar.getInstance();
        currentTime.setTime(date);
        currentTime.add(timeUnit, -amount);
        return currentTime.getTime();
    }

    /**
     * 获取传入日期当天的开始时间
     *
     * @param date
     * @return
     */
    public static Date getStartDate(Date date) throws IllegalArgumentException {
        if (Objects.isNull(date)) {
            throw new IllegalArgumentException("日期参数不能为空");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取传入日期的开始时间
     * 精确到分钟
     * @param date
     * @return
     */
    public static Date getStartDate0(Date date) throws IllegalArgumentException {
        if (Objects.isNull(date)) {
            throw new IllegalArgumentException("日期参数不能为空");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getStartDateM(Date date) throws IllegalArgumentException {
        if (Objects.isNull(date)) {
            throw new IllegalArgumentException("日期参数不能为空");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    
    /**
     * 获取传入日期的结束时间
     * 精确到分钟
     * @param date
     * @return
     */
    public static Date getEndDate0(Date date) throws IllegalArgumentException {
        if (Objects.isNull(date)) {
            throw new IllegalArgumentException("日期参数不能为空");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取传入日期当天的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndDate(Date date) throws IllegalArgumentException {
        if (Objects.isNull(date)) {
            throw new IllegalArgumentException("日期参数不能为空");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.SECOND, -1);
        return calendar.getTime();
    }

    /**
     * 传入时间转换为指定格式
     * @return:
     * @Date: 2020/9/3 3:53 PM
     */
    public static String formatDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String format = formatter.format(date);
        return format;
    }


    /**
     * 日期格式字符串转date
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date parse(String str, String s) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(s);
        return simpleDateFormat.parse(str);
    }
    
    /**
     * 日期格式字符串转date
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date parse(String str) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.parse(str);
    }

}
