package com.jkcredit.invoice.util;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

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

/**
 * @description: 日期时间工具类
 * @author: sunzhaoning
 * @create: 2019-06-19 15:58
 * @version: V1.0
 **/

public class DateUtil {

    /**
     * 时间转换毫秒
     *
     * @param time
     * @return
     */
    public static Long date2Java(String time) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
        DateTime dateTime = DateTime.parse(time, format);
        Date date;
        date = dateTime.toDate();
        return date.getTime();
    }
    /**
     * 字符串转时间
     *
     * @param time
     * @return
     */
    public static Date StringToDate(String time) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(time, format);
        Date date;
        date = dateTime.toDate();
        return date;
    }
    /**
     * 根据时间获取月份
     *
     * @param time
     * @return
     */
    public static String getMonth(String time) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM");
        DateTimeFormatter format1 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(time, format1);
        return dateTime.toString(format);
    }

    public static String dateFormate(String time) {
        String formatDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            formatDate =  sdf.format(sdf2.parse(time).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formatDate;
    }
    public static String getCurrentDateStr() {
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
           return   sdf2.format(new Date().getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 时间格式转换
     * yyyy-MM-dd HH:mm:ss 转 yyyy-MM-dd'T'HH:mm:ss
     * @param time
     * @return
     */
    public static synchronized String dateToTime(String time) {
        //转换前格式
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        //转换后格式
        DateTimeFormatter format1 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
        //将传入时间按照format格式转换为DateTime类型
        DateTime dateTime = DateTime.parse(time, format);
        //转换为format1字符串类型
        return dateTime.toString(format1);
    }

    /**
     * 判断某个时间 是否在某个区间内
     *
     * @param startTime
     * @param endTime
     * @param invalidTime
     * @return
     */
    public static boolean isInvalidTime(String startTime, String endTime, String invalidTime) {
        Date nowDate = null;
        Date beginDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        try {
            beginDate = sdf.parse(startTime);
            endDate = sdf.parse(endTime);
            nowDate = sdf.parse(invalidTime);
        } catch (Exception e) {
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowDate);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginDate);
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        if (date.after(begin) && date.before(end)) {
            return true;
        }
        return false;
    }

    public static String tTimeToDate(String time) {
        //转换前格式
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
        //转换后格式
        DateTimeFormatter format1 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        //将传入时间按照format格式转换为DateTime类型
        DateTime dateTime = DateTime.parse(time, format);
        //转换为format1字符串类型
        return dateTime.toString(format1);
    }

    /**
     * 判断time是否在now的n天之内
     * @param time
     * @param n    正数表示在条件时间n天之后，负数表示在条件时间n天之前
     * @return
     */
    public static boolean belongDate(String time, int n) {
        DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(time, format);
        Date inputTime;

        inputTime = dateTime.toDate();

        //得到日历
        Calendar calendar = Calendar.getInstance();
        //把当前时间赋给日历
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, n);
        //得到n前的时间
        Date before7days = calendar.getTime();

        if (before7days.getTime() < inputTime.getTime()) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 判断给定时间与当前时间相差多少天
     * @param date
     * @return
     */
    public static long getDistanceDays(String date,Date now) {
        DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(date, format);

        long days = 0;
        try {
            //String转Date
            Date time = dateTime.toDate();

            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            days = diff / (60 * 60 * 24);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //正数表示在当前时间之后，负数表示在当前时间之前
        return days;
    }

    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(String smdate,Date bdate)
    {

        long between_days = 0L;
        try {
            DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
            DateTime dateTime = DateTime.parse(smdate, format);

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date smdate1= dateTime.toDate();
            bdate=sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate1);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            between_days=(time1-time2)/(1000*3600*24);
        }  catch (Exception e) {
        e.printStackTrace();
    }


        return Integer.parseInt(String.valueOf(between_days));
    }


    /**
     * 判断给定时间与当前时间相差多少天
     * @param
     * @return
     */
    public static long getDistanceDays(String date1,String date2) {
        DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(date1, format);
        DateTime dateTime1 = DateTime.parse(date2, format);
        long days = 0;
        try {
            //String转Date
            Date time = dateTime.toDate();
            Date now = dateTime1.toDate();
            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            days = diff / (60 * 60 * 24*1000);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //正数表示在当前时间之后，负数表示在当前时间之前
        return days;
    }
    /**
     * 判断给定时间与当前时间相差多少小时
     * @param date
     * @return
     */
    public static long getDistanceHours(String date,Date now) {
        DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(date, format);

        long hours = 0;
        try {
            //String转Date
            Date time = dateTime.toDate();

            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            hours = diff / (60 * 60*1000 );

        } catch (Exception e) {
            e.printStackTrace();
        }
        //正数表示在当前时间之后，负数表示在当前时间之前
        return hours;
    }
}
