package com.fufan.mtsaas.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class DateFormatTool {
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    private static final Logger LOGGER = LoggerFactory.getLogger(DateFormatTool.class);

    /**
     * 字符串转Date
     * @param string  需要转换的字符串
     * @return
     *
     * @Time 2018-03-02
     * @author Liby
     */
    public static Date stringDateFormatToDate( String string) {
        Date data = null;
        try {
            data = new SimpleDateFormat(DATE_FORMAT).parse(string);
        } catch (Exception e) {
            LOGGER.error( "日期：");
            LOGGER.error("字符串转日期出错",e);
        }
        return data;
    }

    public static void main(String[] args) {
        stringDateFormatToDate("2020-08-24");
    }
    /**
     * 字符串转Date
     * @param pattern 转换的格式
     * @param string  需要转换的字符串
     * @return
     *
     * @Time 2018-03-02
     * @author Liby
     */
    public static Date stringToDate(String pattern, String string) {
        try {
            return new SimpleDateFormat(pattern).parse(string);
        } catch (Exception e) {
            LOGGER.error("字符串转日期出错" + "格式：" + pattern + "日期：" + string.toString());
            LOGGER.error("字符串转日期出错",e);
        }
        return null;
    }
    /**
     * 时间转字符串
     * @param date		转换的时间
     * @param pattern	转换的格式
     * @return
     */
    public static String dateToString( String pattern,Date date) {
        try {
            return new SimpleDateFormat(pattern).format(date).trim();
        }catch (Exception e){
            LOGGER.error("字符串转日期出错" + "格式：" + pattern + "日期：" + date);
            LOGGER.error("字符串转日期出错",e);
        }
        return null;
    }


    /**
     * 时间转字符串
     * @param date		转换的时间
     * @return
     */
    public static String dateToDefaultString(Date date) {
        try {
            return new SimpleDateFormat(DATETIME_FORMAT).format(date).trim();
        }catch (Exception e){
            LOGGER.error("字符串转日期出错" + "格式：" + DATETIME_FORMAT + "日期：" + date);
            LOGGER.error("字符串转日期出错",e);
        }
        return null;
    }

    /**
     * 时间转字符串
     * @param date		转换的时间
     * @return
     */
    public static String dateToDateFormatString(Date date) {
        try {
            return new SimpleDateFormat(DATE_FORMAT).format(date).trim();
        }catch (Exception e){
            LOGGER.error("字符串转日期出错" + "格式：" + DATE_FORMAT + "日期：" + date);
            LOGGER.error("字符串转日期出错",e);
        }
        return null;
    }
    /**
     * 日期转换格式
     * @param pattern
     * @param date
     * @return
     *
     * @Time 2018-03-02
     * @author Liby
     */
    public static Date dateFrmat(String pattern , Date date){
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            String str = dateToString(pattern,date);
            Date   d = stringToDate(pattern,str);
            return d;
        }catch (Exception e){
            LOGGER.error("字符串转日期出错" + "格式：" + pattern + "日期：" + date);
            LOGGER.error("字符串转日期出错",e);
        }
        return null;
    }
    /**
     * 给一个日期加上天数 返回新日期
     * @param date
     * @param day
     * @return
     *
     * @Time 2018-03-02
     * @author Liby
     */
    public static Date addDay(Date date,int day) {
        try {
            Calendar calendar =new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,day);
            date=calendar.getTime();
            return date;
        }catch (Exception e){
            LOGGER.error("日期加上某一天数出错" + "日期：" + date + "天数：" + day );
            LOGGER.error("日期加上某一天数出错",e );
        }
        return null;
    }

}
