package net.educoder.ecsonar.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * mapper.stylefeng.guns.modular.lendchain.utils
 *
 * 日期工具类
 * dell
 * 2018/5/22 11:53
 */

public class DateUtils {


    /**
     * 日期加减
     * @param date
     * @param days
     * @return
     */
    public static final Integer addDays(Integer date ,Integer days){
        int resultDate = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            Date date2 = simpleDateFormat.parse(date.toString());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date2);
            calendar.add(Calendar.DATE,days);
            resultDate = Integer.valueOf(simpleDateFormat.format(calendar.getTime().getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultDate;
    }



    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    //获取年月日时分(H：24小时制，h：12小时制)
    public static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMMddHHmm");
    //获取秒
    public static SimpleDateFormat dateFormat3 = new SimpleDateFormat("ss");





    /**
     * @Description: 获取当日的开始时间戳
     * @Method:
     * @Param:
     * @return:
     * @throws:
     * @Author:
     * @Date:   2018/5/22 9:47
     */
    public static Long getStartTime(){
        long startTime = (System.currentTimeMillis()/86400000 )*86400000 - 8*60*60*1000;
        return startTime / 1000;
    }

    /**
     * @Description: 获取当日的截止时间戳
     * @Method:
     * @Param:
     * @return:
     * @throws:
     * @Author:
     * @Date:   2018/5/22 9:47
     */
    public static Long getEndTime(){
        long endTime = (System.currentTimeMillis()/86400000 )*86400000 - 8*60*60*1000 + 86400000;
        return endTime / 1000;
    }



    /**
     * 根据Unix时间戳获取时间字符串
     * @param unixTimestamp
     * @return
     */
    public static String formatDate(Integer unixTimestamp){
        return getDateByTimestamp(unixTimestamp,"yyyy-MM-dd HH:mm:ss","Asia/Shanghai");
    }

    public static String formatDate(Date date){
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        ZoneId zid = ZoneId.of("Asia/Shanghai");
        sd.setTimeZone(TimeZone.getTimeZone(zid));
        return sd.format(date);
    }

    /**
     * 根据Unix时间戳获取时间字符串
     * @param unixTimestamp
     * @param format
     * @return
     */
    public static String formatDate(Integer unixTimestamp,String format){
        return getDateByTimestamp(unixTimestamp,format,"Asia/Shanghai");
    }

    /**
     * 根据Unix时间戳获取时间字符串
     * @param unixTimestamp
     * @param format
     * @param zone
     * @return
     */
    public static String getDateByTimestamp(Integer unixTimestamp, String format, String zone){
        SimpleDateFormat sd = new SimpleDateFormat(format);
        ZoneId zid = ZoneId.of(zone);
        sd.setTimeZone(TimeZone.getTimeZone(zid));
        Long create_time = ((long)unixTimestamp)* 1000;
        return sd.format(new Date(create_time));
    }


    /**
     * 日期加减
     * @param timestamp
     * @param minutes
     * @return
     */
    public static final String addMinutes(String timestamp ,Integer minutes){
        String resultDate = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date2 = simpleDateFormat.parse(timestamp);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date2);
            calendar.add(Calendar.MINUTE,minutes);
            resultDate = simpleDateFormat.format(calendar.getTime().getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultDate;
    }

    public static final String addMin(String timestamp ,Integer minutes){
        String resultDate = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date2 = simpleDateFormat.parse(timestamp);
            System.out.println("格式化后的日期为："+date2);
            resultDate = String.valueOf(date2.getTime()/1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultDate;
    }

}