package com.zhaohaoting.framework.utils;

import android.text.TextUtils;
import android.util.Log;

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

/**
 * author：Created by zhaohaoting on 2018/1/10 11:51
 * email：526309416@qq.com
 * desc：
 */

public class DateUtils {
    public static final String TAG = "DateUtils";
    /**
     * 根据日期获得星期
     *
     * @param date
     * @return
     */
    public static int getWeekOfDate(Date date) {
        // String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return intWeek;
    }
    /**
     * 将日期字符串由一种格式转换为另一种格式
     *
     * @param timeStr      时间字符串
     * @param sourceFormat 时间字符串格式
     * @param destFormat   目标时间字符串格式
     * @return
     */
    public static String getFormatTime(String timeStr, String sourceFormat, String destFormat) {
        if (TextUtils.isEmpty(timeStr)) {
            return "";
        }
        try {
            return new SimpleDateFormat(destFormat, Locale.CHINA).format(new SimpleDateFormat(sourceFormat, Locale.CHINA).parse(timeStr));
        } catch (Exception e) {
            //e.printStackTrace();
            Log.e(TAG, "将" + timeStr + "由格式" + sourceFormat + "转换为" + destFormat + "格式出现错误：" + e.getMessage());
            return "";
        }
    }

    /**
     * 获取当前几周
     *
     * @return
     */
    public static int getCurrentDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        int dayOfWeek = calendar.get(calendar.DAY_OF_WEEK) - 1;//(0-6)
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        }
        return dayOfWeek;
    }

    /**
     * 获取当前几周
     *
     * @return
     */
    public static int getCurrentDayOfWeek(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int dayOfWeek = calendar.get(calendar.DAY_OF_WEEK) - 1;//(0-6)
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        }
        return dayOfWeek;
    }

    /**
     * 获取当前几周
     *
     * @return
     */
    public static String getCurrentDayOfWeekString(long time) {
        String[] dayOfWeekStrings = {"日", "一", "二", "三", "四", "五", "六"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int dayOfWeek = calendar.get(calendar.DAY_OF_WEEK) - 1;//(0-6)
        return "星期"+dayOfWeekStrings[dayOfWeek];
    }

    /**
     * 获取当前月第一天：
     *
     * @return
     */
    public static String getCurrentMonthFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String first = format.format(c.getTime());
        return first + " 00:00:00";
    }

    /**
     * 获取当前月最后一天
     *
     * @return
     */
    public static String getCurrentMonthLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = format.format(ca.getTime());
        return last + " 23:59:59";
    }

    /***
     * 获取本周第一天
     * @return
     */
    public static String getCurrentWeekFirstDay() {
        int FIRST_DAY = Calendar.MONDAY;
        Calendar cal = Calendar.getInstance();
        while (cal.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
            cal.add(Calendar.DATE, -1);
        }
        String time = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(cal.getTime());
        return time + " 00:00:00";
    }

    /***
     * 获取本周最后一天
     * @return
     */
    public static String getCurrentWeekLastDay() {
        int FIRST_DAY = Calendar.MONDAY;
        Calendar cal = Calendar.getInstance();
        while (cal.get(Calendar.DAY_OF_WEEK) != FIRST_DAY) {
            cal.add(Calendar.DATE, -1);
        }
        cal.add(Calendar.DATE, 6);
        String time = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE).format(cal.getTime());
        return time + " 23:59:59";
    }

    /**
     * 获取上周第一天
     *
     * @return
     */
    public static String getLastWeekFirstDay() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一，默认是星期天
        cal.add(Calendar.DATE, -1 * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA).format(cal.getTime());
        return monday + " 00:00:00";
    }

    /**
     * 获取上周最后一天
     *
     * @return
     */
    public static String getLastWeekLastDay() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一，默认是星期天
        cal.add(Calendar.DATE, -1 * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String sunday = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA).format(cal.getTime());
        return sunday + " 23:59:59";
    }

    /**
     * 获取下周第一天
     *
     * @return
     */
    public static String getNextWeekFirstDay() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一，默认是星期天
        cal.add(Calendar.DATE, 1 * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        String monday = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA).format(cal.getTime());
        return monday + " 00:00:00";
    }

    /**
     * 获取上周最后一天
     *
     * @return
     */
    public static String getNextWeeklastDay() {
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);//将每周第一天设为星期一，默认是星期天
        cal.add(Calendar.DATE, 1 * 7);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String sunday = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA).format(cal.getTime());
        return sunday + " 23:59:59";
    }

    /**
     * 得到昨天时间
     *
     * @return yyyy-MM-dd
     */
    public static String getLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.DATE, -1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return formatter.format(calendar.getTime());
    }

    /**
     * 得到明天时间
     *
     * @return yyyy-MM-dd
     */
    public static String getNextDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.DATE, 1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return formatter.format(calendar.getTime());
    }

    /**
     * yyyy-MM-dd HH:mm:ss格式字符串
     *
     * @param stringDate
     * @return
     */
    public static String secondFormatMin(String stringDate) {
        SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.CHINA);

        try {
            return dateFormat2.format(dateFormat1.parse(stringDate));
        } catch (ParseException e) {
            e.printStackTrace();
            return stringDate;
        }
    }
}
