package com.zbf.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @program:
 * @description:
 * @author: Tony.wu
 * @create: 2022-10-13 16:29
 **/
@Slf4j
public class CommonUtils {

    /**
     * 判断是否是数字
     *
     * @param word
     * @return
     */
    public final static boolean isNumeric(Object word) {
        if (word == null || "".equals(word)) {
            return false;
        }
        String result = String.valueOf(word);
        return result.matches("^((-)?\\d?|\\.)?(\\d|\\.\\d+)+$");
    }



    public final static Date getDate(Object date) {
        Date result = null;
        if (date != null) {
            if (!"".equals((String) date)) {
                result = DateUtil.fomatDate((String) date, "sdfDay");
            }
        }
        return result;
    }



    //日期转字符串
    public final static String formatDate(Date date) {
        SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd");
        return dateTimeFormatter.format(date);
    }

    //字符串转日期
    public final static Date parseDate(String date, String type) {
        SimpleDateFormat dateTimeFormatter = null;
        if (StringUtils.isBlank(type)) {
            dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd");
        } else {
            dateTimeFormatter = new SimpleDateFormat(type);
        }
        try {
            return dateTimeFormatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }









    /**
     * 处理特殊类型日期
     * 例如：2023-12-12T17:16:12.000+0800
     *
     * @param oldDate
     * @return
     */
    public static String dealDateFormat(Date oldDate) {
        Date date1 = null;
        DateFormat df2 = null;
        try {
//            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//            Date date = df.parse(oldDate);
            SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(oldDate.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return df2.format(date1);
    }

    public static void main(String[] args) {
        Date date = CommonUtils.parseDate("2023-12-23", "yyyy-MM");
        System.out.println(DateUtil.getMonth(date));
    }


}
