package com.rrtx.services.common.utils;

import cn.hutool.core.util.StrUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;


/**
 * ClassName: DateUtil<br>
 * Description: 时间工具类 <br>
 * Company: rrtx
 *
 * @author rrtx
 * @version v1.0.0    2019/7/24 5:05 PM  xugz    文件初始创建
 */
public class DateUtil {

	public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";

	public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static final String CST_TIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy";

	public static final String FULL_TIME_SPLIT_DATE = "yyyy-MM-dd";

	public static final String FULL_TIME_SPLIT_TIME = "HH:mm:ss";

	/**
	 * 根据指定格式格式化日期
	 * @return
	 */
	public static String formatDate(String dateStr, String format) {
		if(StrUtil.isEmpty(dateStr)) {
			return "";
		}
		Date date = cn.hutool.core.date.DateUtil.parse(dateStr);
		String formatStr = cn.hutool.core.date.DateUtil.format(date, format);
		return formatStr;
	}

	/**
	 * HH:mm:ss
	 * @return
	 */
	public static String getCurrentTimeFormt() {
		Date date = cn.hutool.core.date.DateUtil.date();
		String format = cn.hutool.core.date.DateUtil.format(date, FULL_TIME_SPLIT_TIME);
		return format;
	}

	/**
	 * yyyy-MM-dd
	 * @return
	 */
	public static String getCurrentDateFormt() {
		Date date = cn.hutool.core.date.DateUtil.date();
		String format = cn.hutool.core.date.DateUtil.format(date, FULL_TIME_SPLIT_DATE);
		return format;
	}
	/**
	 * yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getFormatCurrentTime() {
		Date date = cn.hutool.core.date.DateUtil.date();
		String format = cn.hutool.core.date.DateUtil.format(date, FULL_TIME_SPLIT_PATTERN);
		return format;
	}

	/**
	 * yyyyMMddHHmmss
	 * @return
	 */
	public static String getCurrentTime() {
		Date date = cn.hutool.core.date.DateUtil.date();
		String format = cn.hutool.core.date.DateUtil.format(date, FULL_TIME_PATTERN);
		return format;
	}
	/**
	 * yyyyMMdd
	 * @return
	 */
	public static String getCurrentDate() {
		Date date = cn.hutool.core.date.DateUtil.date();
		String format = cn.hutool.core.date.DateUtil.format(date, "yyyyMMdd");
		return format;
	}
	public static String formatFullTime(LocalDateTime localDateTime) {
		return formatFullTime(localDateTime, FULL_TIME_PATTERN);
	}

	public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
		return localDateTime.format(dateTimeFormatter);
	}

	public static String getDateFormat(Date date, String dateFormatType) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatType, Locale.CHINA);
		return simpleDateFormat.format(date);
	}

	public static String formatCSTTime(String date, String format)  {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(CST_TIME_PATTERN, Locale.US);
		Date usDate = null;
		try {
			usDate = simpleDateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return DateUtil.getDateFormat(usDate, format);
	}

	public static String formatInstant(Instant instant, String format) {
		LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
		return localDateTime.format(DateTimeFormatter.ofPattern(format));
	}

	/**
	 * 取得指定时间间隔后的系统时间
	 *
	 * @param hour
	 *            小时
	 * @return String
	 */
	public static  String getDifferentTime( int hour )
	{
		GregorianCalendar calendar = (GregorianCalendar) Calendar.getInstance();
		calendar.add( Calendar.HOUR, hour );
		SimpleDateFormat formatter = new SimpleDateFormat( FULL_TIME_PATTERN );
		return formatter.format( calendar.getTime() );
	}
// 比较2个日期：返回－1:开始日期小于结束日期，1：开始日期大于结束日期，0：开始日期等于结束日期
	/**
	 * @param StartDate
	 * @param EndDate
	 * @return
	 */
	public static int DateCompare(String StartDate, String EndDate) {

		Date start = String2Date(StartDate);
		Date end = String2Date(EndDate);

		if (start.before(end)) {
			return -1;
		} else if (start.after(end)) {
			return 1;
		} else
			return 0;

	}
	// 返回年月日方式或者年月日时分方式对应的日期时间
	/**
	 * @param str
	 * @return
	 */
	public static Date String2Date(String str) {
		try {
			SimpleDateFormat s1 = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat s2 = new SimpleDateFormat("yyyyMMddHHmm");
			SimpleDateFormat s3 = new SimpleDateFormat("yyyyMMddHHmmss");

			if (str.length() == 8) {
				return s1.parse(str);
			} else if (str.length() == 12) {
				return s2.parse(str);
			} else if (str.length() == 14) {
				return s3.parse(str);
			} else {
				throw new RuntimeException("validation.date.parse_error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("adsasd");
		}

	}
	/**
	 * 取得指定时间间隔后的系统时间
	 *
	 * @param minutes
	 *
	 * @return String
	 */
	public static  String getDifferentTimeByMinute( int minutes )
	{
		GregorianCalendar calendar = (GregorianCalendar) Calendar.getInstance();
		calendar.add( Calendar.MINUTE, minutes);
		SimpleDateFormat formatter = new SimpleDateFormat( FULL_TIME_PATTERN );
		return formatter.format( calendar.getTime() );
	}
	public static String getWeek(String date) throws ParseException {
		String week = "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
		Date today = simpleDateFormat.parse(date);
		Calendar c = Calendar.getInstance();
		c.setTime(today);
		int weekday = c.get(Calendar.DAY_OF_WEEK);
		if (weekday == 1) {
			week = "周日";
		} else if (weekday == 2) {
			week = "周一";
		} else if (weekday == 3) {
			week = "周二";
		} else if (weekday == 4) {
			week = "周三";
		} else if (weekday == 5) {
			week = "周四";
		} else if (weekday == 6) {
			week = "周五";
		} else if (weekday == 7) {
			week = "周六";
		}
		return week;
	}
}
