package com.fkzy.warn.common.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间工具类
 * @author yuyantian
 * @date 2020/05/07
 */
public class TimeUtil {


  /**
   * 时间戳字符串转时间
   *
   * @param timestamp 毫秒
   */
  public static Date formatTimestampToDate(String timestamp) {
    Date date = null;
    if (StringUtils.isNotBlank(timestamp)) {
      Long time = Long.parseLong(timestamp);
      date = new Date(time);
    }
    return date;
  }

  /**
   * 获取当天时间字符串
   */
  public static String getTodayTimeStr() {
    String result = null;
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Date date = new Date();
      result = sdf.format(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }

  /**
   * 获取明天时间字符串
   */
  public static String getTomorrowTimeStr() {
    String result = null;
    SimpleDateFormat sdf = null;
    Calendar c = null;
    try {
      sdf = new SimpleDateFormat("yyyy-MM-dd");
      c = new GregorianCalendar();
      c.setTime(new Date());
      c.add(c.DATE, 1);
      result = sdf.format(c.getTime());
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }

  /**
   * 获取当日时间字符串 yyyyMMdd
   */
  public static String formatTimeyyyyMMdd() {
    String result = null;
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
      Date date = new Date();
      result = sdf.format(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }

  /**
   * 将时间字符串转换成时间戳字符串
   *
   * @param timeStr yyyy-MM-dd HH:mm:ss
   */
  public static String formatTimeToTimestamp(String timeStr) {
    String timestamp = "";
    if (StringUtils.isNotBlank(timeStr)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
        Date d = sdf.parse(timeStr);
        timestamp = d.getTime() + "";
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return timestamp;
  }

  /**
   * 将时间字符串转换成日期时间
   *
   * @param timeStr yyyy-MM-dd HH:mm:ss
   */
  public static Date formatTimeToDate(String timeStr) {
    Date date = null;
    if (StringUtils.isNotBlank(timeStr)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
        date = sdf.parse(timeStr);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return date;
  }


  /**
   * 将时间字符串转换成日期时间
   *
   * @param timeStr yyyy-MM
   */
  public static Date formatyyyyMMTimeToDate(String timeStr) {
    Date date = null;
    if (StringUtils.isNotBlank(timeStr)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
      try {
        date = sdf.parse(timeStr);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return date;
  }

  /**
   * 将时间字符串转换成日期时间
   *
   * @param timeStr yyyy-MM-dd
   */
  public static Date formatyyyyMMddTimeToDate(String timeStr) {
    Date date = null;
    if (StringUtils.isNotBlank(timeStr)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      try {
        date = sdf.parse(timeStr);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return date;
  }
  /**
   * 将时间转换成字符串
   *
   * @param time yyyy-MM-dd HH:mm:ss
   */
  public static String formatTimeToDate(Date time) {
    String timeStr = null;
    if (null!=time) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
        timeStr = sdf.format(time);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return timeStr;
  }

  /**
   * 将时间转换成字符串
   *
   * @param time yyyyMMddHHmmss
   */
  public static String formatTimeyyyyMMddHHmmss(Date time) {
    String timeStr = null;
    if (null!=time) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
      try {
        timeStr = sdf.format(time);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return timeStr;
  }

  /**
   * 时间转字符串
   *
   * @param date yyyy-MM-dd
   */
  public static String formatTimeStr(Date date) {
    String timeStr = null;
    if(null !=date){
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      try {
        timeStr = sdf.format(date);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    return timeStr;
  }

  /**
   * 时间转字符串
   *
   * @param date yyyy-MM-dd
   */
  public static String formatTimeStryyyyMMddHHmmssSSS(Date date) {
    String timeStr = null;
    if(null !=date){
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
      try {
        timeStr = sdf.format(date);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return timeStr;
  }

  /**
   * 时间转字符串
   *
   * @param date yyyy-MM-dd
   */
  public static String formatTimeStryyyyMMddHHmm(Date date) {

    String timeStr = null;
    if(null!=date){
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      try {
        timeStr = sdf.format(date);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return timeStr;
  }

  public static String formatTimeStryyyyMMddHHmmssSSSNum(Date date) {
    String timeStr = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    try {
      timeStr = sdf.format(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return timeStr;
  }

  /**
   * 获取明天时间
   *
   * @param date yyyy-MM-dd
   */
  public static String formatTomorrow(Date date) {
    String timeStr = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.DAY_OF_MONTH, +1);//时间加一天
      Date tomorrow = calendar.getTime();
      timeStr = sdf.format(tomorrow);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return timeStr;
  }

  /**
   * 获取月底时间
   *
   * @param date yyyy-MM-dd
   */
  public static String getNextMonthFirst(Date date) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DATE, 0);
    return df.format(calendar.getTime());
  }


  /**
   * 获取计算相差天数的时间
   *
   * @param timeStr yyyy-MM-dd
   */
  public static String formatEndTime(String timeStr,int value) {
    if (StringUtils.isNotBlank(timeStr)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      try {
        Date d = sdf.parse(timeStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.add(Calendar.DAY_OF_MONTH, +value);//时间加一天
        return sdf.format(calendar.getTime());
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return null;
  }

  /**
   * 时间字符串转时间
   *
   * @param dateStr yyyy-MM-dd
   */
  public static Date formatDate(String dateStr) {
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
      date = sdf.parse(dateStr);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return date;
  }

  /**
   * 日期转换成时间字符串
   * yyyyMMddHHmmss
   */
  public static String formatDateToTime(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String time = sdf.format(date);
    return time;
  }

  /**
   * 获取当月第一天
   *
   * @return yyyy-MM-dd
   */
  public static String getMonthOfFirst() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cale = Calendar.getInstance();
    cale.add(Calendar.MONTH, 0);
    cale.set(Calendar.DAY_OF_MONTH, 1);
    String monthFirst = sdf.format(cale.getTime());
    return monthFirst;
  }

  /**
   * 获取当年第一天
   *
   * @return yyyy-MM-dd
   */
  public static String getYearOfFirst() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cale = Calendar.getInstance();
    cale.add(Calendar.YEAR, 0);
    cale.set(Calendar.DAY_OF_YEAR, 1);
    String yearFirst = sdf.format(cale.getTime());
    return yearFirst;
  }


  /**
   * 时间字符串转日期
   * yyyy-MM-dd
   */
  public static Date dateStrFormatDate(String dateStr) {
    Date d = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
      d = sdf.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return d;
  }

  /**
   * yyyy年MM月dd日转换为时间
   * @param dateStr
   * @return
   */
  public static Date format1(String dateStr){
    Date d = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
    try {
      d = sdf.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return d;
  }

  /**
   * 参数为时间类型，获取该时间为星期几
   * @param date
   * @return
   */
  public static String getWeek(Date date){
    String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
    if(week_index<0){
      week_index = 0;
    }
    return weeks[week_index];
  }

  /**
   * 参数为字符串类型，获取该时间为星期几 yyyy年MM月dd日格式的字符串
   * @param dateStr
   * @return
   */
  public  static String getWeek(String dateStr){
    Date date = formatDate(dateStr);
    String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
    if(week_index<0){
      week_index = 0;
    }
    return weeks[week_index];
  }

  public static Date formatyyyyMMddHHmmTimeToDate(String timeStr) {
    Date date = null;
    if (StringUtils.isNotBlank(timeStr)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
      try {
        date = sdf.parse(timeStr);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    return date;
  }


  /**
   * 获取指定
   * @param year
   * @param month
   * @return
   */
  public static String getLastDayOfMonth(int year,int month)
  {
    Calendar cal = Calendar.getInstance();
    //设置年份
    cal.set(Calendar.YEAR,year);
    //设置月份
    cal.set(Calendar.MONTH, month);
    //获取当月最小值
    int lastDay = cal.getMinimum(Calendar.DAY_OF_MONTH);
    //设置日历中的月份，当月+1月-1天=当月最后一天
    cal.set(Calendar.DAY_OF_MONTH, lastDay-1);
    //格式化日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String lastDayOfMonth = sdf.format(cal.getTime());
    return lastDayOfMonth;
  }



  /**
   * 获取年份+月份
   *
   * @param amount 月份+amount
   * @return
   */
  public static Integer getMonth(int amount) {
    String month = "";
    SimpleDateFormat sdf = new SimpleDateFormat("MM");
    Calendar calendar = Calendar.getInstance();
    //获取月份
    calendar.add(Calendar.MONTH, amount);
    month = sdf.format(calendar.getTime());
    return  Integer.parseInt(month);

  }

  /**
   * 获取当年月份集合
   *
   * @return
   */
  public static List<Integer> getNowYearMonthList(int amount) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    SimpleDateFormat sdf = new SimpleDateFormat("MM");//格式化为年月
    Calendar min = Calendar.getInstance();
    Calendar max = Calendar.getInstance();
    min.add(Calendar.MONTH, 0);
    min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
    min.add(Calendar.MONTH, -amount);
    max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
    Calendar curr = min;
    while (curr.before(max)) {
      result.add(Integer.parseInt(sdf.format(curr.getTime())));
      curr.add(Calendar.MONTH, 1);
    }
    return result;
  }

  /**
   * 得到当前日期的年份
   * @return
   */
  public static String nowYear() {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
    String times = sdf.format(date);
    return times;
  }
  public static String getYear(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("YYYY");
    String times = sdf.format(date);
    return times;
  }

  public static String getMonth(Date date) {
    SimpleDateFormat sdf = new SimpleDateFormat("MM");
    String times = sdf.format(date);
    return times;
  }

  /**
   * 获取近amount年份集合
   *
   * @return
   */
  public static List<Integer> getYearList(int amount) {
    List<Integer> result = new ArrayList<>();
    String currYear = nowYear();
    if(!TextUtils.isEmpty(currYear)){
      int currYear_int = Integer.parseInt(currYear);
      result.add(currYear_int);
      for (int i = 0;i < amount-1 ;i++){
        currYear_int--;
        result.add(currYear_int);
      }
    }
    return result;
  }

  public static List<Integer> getYearList(int amount,int nowYear) {
    List<Integer> result = new ArrayList<>();
    String currYear = String.valueOf(nowYear);
    if(!TextUtils.isEmpty(currYear)){
      int currYear_int = Integer.parseInt(currYear);
      result.add(currYear_int);
      for (int i = 0;i < amount-1 ;i++){
        currYear_int--;
        result.add(currYear_int);
      }
    }
    return result;
  }

  public static void main(String[] args) {
    String dateStr="2023/01/01";
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    try {
      date = sdf.parse(dateStr);
      System.out.println(date);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 将指定的天数添加到给定的日期上
   *
   * @param date
   * @param days    要增加的天数
   * @return 增加天数后的日期字符串
   * @throws ParseException 如果日期字符串格式不正确，则抛出异常
   */
  public static Date addDaysToDate(Date date, int days)  {

    // 使用Calendar来操作日期
    try{
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.DAY_OF_MONTH, days); // 增加天数
      return calendar.getTime();
    }catch (Exception e){
      e.printStackTrace();
    }
    return date;
  }



 /**
   * 获取系统时间
   * @param
   * @return
  */
  public  static Date getSystemDate(){
    return new Date();
  }
}
