package com.casic.dcms.utils;

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

/**
 * @author : Pengxh
 * @time : 2021/4/15 16:54
 * @email : 290677893@qq.com
 **/
public class TimeOrDateUtil {
    private static final String TAG = "TimeOrDateUtil";
    private static SimpleDateFormat dateFormat;

    /**
     * 获取年月区间,[2022-02-01,2022-02-28]
     */
    public static String[] timestampToMonth(long millSeconds) throws ParseException {
        dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        //先判断是否在当月
        if (isInThisMonth(millSeconds)) {
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            //获得本月第一天
            calendar.add(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            String firstDay = dateFormat.format(calendar.getTime());

            date.setTime(millSeconds);
            String currentDay = dateFormat.format(date);
            return new String[]{firstDay, currentDay};
        } else {
            String dateValue = dateFormat.format(new Date(millSeconds));
            Date date = dateFormat.parse(dateValue);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            //获得月第一天
            calendar.add(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            String firstDay = dateFormat.format(calendar.getTime());

            int year = calendar.get(Calendar.YEAR);
            int m = calendar.get(Calendar.MONTH) + 1;
            String month;
            if (m < 10) {
                month = "0" + m;
            } else {
                month = String.valueOf(m);
            }
            int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            return new String[]{firstDay, year + "-" + month + "-" + maxDay};
        }
    }

    /**
     * 判断时间是否在本月之内
     */
    public static boolean isInThisMonth(long millSeconds) {
        //所选时间对应的月份
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM", Locale.CHINA);
        String selectedMonth = dateFormat.format(new Date(millSeconds));
        //系统时间对应的月份
        String systemMonth = dateFormat.format(new Date(System.currentTimeMillis()));
        return selectedMonth.equals(systemMonth);
    }

    /**
     * 时间戳转年月
     */
    public static String timestampToYearMonth(long millSeconds) {
        dateFormat = new SimpleDateFormat("yyyy年MM月", Locale.CHINA);
        return dateFormat.format(new Date(millSeconds));
    }

    /**
     * 时间戳转时分
     */
    public static String minuteToTime(long millSeconds) {
        dateFormat = new SimpleDateFormat("HH:mm", Locale.CHINA);
        return dateFormat.format(new Date(millSeconds));
    }

    public static String getOneMonthDate() {
        dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        long oneMonth = System.currentTimeMillis() - 31 * 24 * 3600 * 1000L;
        return dateFormat.format(new Date(oneMonth));
    }

    public static String getThreeMonthDate() {
        dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        long oneMonth = System.currentTimeMillis() - 3 * 31 * 24 * 3600 * 1000L;
        return dateFormat.format(new Date(oneMonth));
    }

    public static String getSixMonthDate() {
        dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        long oneMonth = System.currentTimeMillis() - 6 * 31 * 24 * 3600 * 1000L;
        return dateFormat.format(new Date(oneMonth));
    }
}
