package com.hw.nativeapp.utils;

import android.util.Log;

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

/**
 * @author yi.ding
 * @description
 * @create 2017/9/9 11:44
 */

public class DateUtils {
   public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public DateUtils() {
    }

    public static int daysBetween(Date smdate, Date bdate) {
        long between_days = 0L;

        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd");
            smdate = e.parse(e.format(smdate));
            bdate = e.parse(e.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            between_days = (time2 - time1) / 86400000L;
        } catch (ParseException var10) {
            var10.printStackTrace();
        }

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

    public static boolean isBetween(Date date, Date startDate, Date endDate) {
        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            startDate = e.parse(e.format(startDate));
            endDate = e.parse(e.format(endDate));
            date = e.parse(e.format(date));
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            long time0 = cal.getTimeInMillis();
            cal.setTime(startDate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(endDate);
            long time2 = cal.getTimeInMillis();
            if(time0 > time1 && time0 <= time2) {
                return true;
            }
        } catch (ParseException var11) {
            var11.printStackTrace();
        }

        return false;
    }

    public static boolean isToday(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String _date = sdf.format(date);
        String _today = sdf.format(new Date());
        return _date.equals(_today);
    }

    public static long DifferMillis(Date sDate, Date eDate) {
        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sDate = e.parse(e.format(sDate));
            eDate = e.parse(e.format(eDate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(sDate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(eDate);
            long time2 = cal.getTimeInMillis();
            return Math.abs(time1 - time2);
        } catch (ParseException var8) {
            var8.printStackTrace();
            return -1L;
        }
    }

    public static long DifferSeconds(Date sDate, Date eDate) {
        return DifferMillis(sDate, eDate) / 1000L;
    }

    public static long DifferMinutes(Date sDate, Date eDate) {
        return Long.parseLong(String.valueOf(DifferMillis(sDate, eDate) / 60000L));
    }

    public static String getNowDate(String format) {
        return (new SimpleDateFormat(format)).format(new Date());
    }

    public static String getTomorrowDate(String format) {
        return (new SimpleDateFormat(format)).format(addDate(new Date(), 1));
    }

    public static String getDateString(Date date, String format) {
        if (date == null){
            return "";
        }
        return (new SimpleDateFormat(format)).format(date);
    }

    public static Date parse(String date, String format) {
        try {
            return (new SimpleDateFormat(format)).parse(date);
        } catch (ParseException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static boolean nowTimeBetween(String beginTimeStr, String endTimeStr) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
        Long beginTime = Long.valueOf(dateFormat.parse(beginTimeStr).getTime());
        Long endTime = Long.valueOf(dateFormat.parse(endTimeStr).getTime());
        Long nowTime = Long.valueOf(dateFormat.parse(dateFormat.format(new Date())).getTime());
        return nowTime.longValue() >= beginTime.longValue() && nowTime.longValue() <= endTime.longValue();
    }

    public static Date addDate(Date date, int days) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(5, days);
        return calendar.getTime();
    }

    public static Date addHour(Date date, int hour) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(10, hour);
        return calendar.getTime();
    }

    public static Date addMinute(Date date, int minute) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(12, minute);
        return calendar.getTime();
    }

    public static Date addSecond(Date date, int second) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(13, second);
        return calendar.getTime();
    }

    public static Date getDate(String timestamp) {
        return getDateBySecond(Long.parseLong(timestamp));
    }

    public static Date getDateByString(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Date getDateByMillis(long timestamp) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(timestamp);
        return calendar.getTime();
    }

    public static Date getDateBySecond(long timestamp) {
        return getDateByMillis(timestamp * 1000L);
    }

    public static long getTimestamp(Date date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }

    public static long DifferMillis(String timestamp, String nowTimestamp) {
        Date date1 = parse(timestamp, "yyyy-MM-dd HH:mm:ss");
        Date date2 = parse(nowTimestamp, "yyyy-MM-dd HH:mm:ss");
        return DifferMillis(date1, date2);
    }

    public static long DifferMillis(String timestamp, Date date) {
        Date date1 = parse(timestamp, "yyyy-MM-dd HH:mm:ss");
        return DifferMillis(date1, date);
    }


    public static Date getCurrentTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        return new Date();
    }


    /**
     * 获取某一天00:00:00
     *
     * @param date
     *            年份
     * @return Date
     */
    public static String getSomeDayStart(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        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 df.format(calendar.getTime());
    }

    /**
     * 获取某一天23:59:59
     *
     * @param date
     *            年份
     * @return Date
     */
    public static String getSomeDayEnd(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return df.format(calendar.getTime());
    }

    /**
     * 获取某周第一天日期
     *
     * @param date
     *            年份
     * @return Date
     */
    public static String getFirstDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){//一周开始为星期天:周日获取上一周星期一
            calendar.setTime(new Date(date.getTime() - 24*3600*1000l));
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期
        return df.format(calendar.getTime());
    }

    /**
     * 获取某周最后一天日期
     *
     * @param date
     *            年份
     * @return Date
     */
    public static String getLastDayOfWeek(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){//一周开始为星期天:周日获取上一周星期一
            calendar.setTime(new Date(date.getTime() - 24*3600*1000l));
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        calendar.add(Calendar.WEEK_OF_YEAR, 1);
        return sdf.format(calendar.getTime());

    }

    /**
     * 转换星期
     *
     * @param dateStr
     *            年份
     */
    public static String getWeek(String dateStr) throws ParseException {
        // 创建 Calendar 对象并设置指定日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateStr);

        int dayOfWeek = date.getDay(); // 获取星期几（从1开始计数）
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

        return weekDays[dayOfWeek];
    }

    public static void main(String[] argc){
        Long time = new Date().getTime() + 72*3600*1000;
        String date = getFirstDayOfWeek(new Date(time));

        String lastDate = getLastDayOfWeek(new Date(time));
        System.out.println("当前时间:" + DateUtils.getDateString(new Date(time),"yyyy-MM-dd HH:mm:ss"));
        System.out.println("日期时间:" +date + "   " + lastDate);
    }

    /**
     * 2020-10-12T11:22:49.000+0800字符串 转 2020-10-12 11:22:49 字符串
     * @param oldDateStr
     * @return
     */
    public static String toDateStr(String oldDateStr){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = null;
        try {
            date = formatter.parse(oldDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newDateStr=sdf.format(date);
        return newDateStr;
    }
}
