package com.lantone.qc.dbanaly.facade.yiwu.util;

import com.lantone.qc.pub.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

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

public class YWDateUtils {
    public static void main(String[] args) {
//        String strDateTime="20150206201756";
//        System.out.println(convJsonDateToString(strDateTime,"yyyy-MM-dd HH:mm:ss"));
        System.out.println(getCurrentTime());
    }
    public static String convJsonDateToString(String jsonDate,String stringTemplate){
        String resultString=null;
        try {
            Calendar cldCalendar=convToCalender(jsonDate, "yyyyMMddhhmmss");
            resultString=convToString(cldCalendar, stringTemplate);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultString;
    }
    public static String convJsonDateToString(String jsonDate,String jsonTemplate,String stringTemplate){

        String resultString=null;
        try {
            Calendar cldCalendar=convToCalender(jsonDate, jsonTemplate);
            resultString=convToString(cldCalendar, stringTemplate);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultString;
    }
    // 把指定格式的日期字符串转日期类型
    public static Calendar convToCalender(String str,String template){

        SimpleDateFormat sdf;
        Date date;
        Calendar cltResult = Calendar.getInstance();

        sdf = new SimpleDateFormat(template, Locale.getDefault());
        try {
            date = sdf.parse(str);

            cltResult.setTime(date);

        } catch (Exception ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        }

        return cltResult;
    }
    // 把日期转指定格式字符串
    public static String convToString(Calendar cld,String template){
        String resultString=null;
        try {
            Date date=cld.getTime();
            SimpleDateFormat sdf=new SimpleDateFormat(template,Locale.getDefault());
            resultString=sdf.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return resultString;
    }
    public static String getCurrentTime(){
        DateFormat df = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT_NO_SPLIT);
        Calendar calendar = Calendar.getInstance();
        return df.format(calendar.getTime());
    }

    public static Date parseDateTime(String datetime) {
        Date date = null;
        try {
            datetime = remove_ctl(datetime);

            if (datetime.contains("至")) {
                datetime = datetime.split("至")[1].replaceAll("[\\u4e00-\\u9fa5]", "");
            }

            if (datetime.endsWith(":")) {
                datetime = datetime.substring(0, datetime.length() - 1);
            }

            if (datetime.length() > 0 && !"-".equals(datetime)) {
                date = DateUtils.parseDate(datetime, dateFormats);
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return date;
    }

    public static String remove_ctl(String str) {
        String trim = "";
        if (StringUtils.isNotEmpty(str)) {
            trim = str.replaceAll("\r|\n|\r\n|/r/n", "").trim();
        }
        return trim;
    }

    public static String[] dateFormats = {
            "yyyy年MM月dd日HH时mm分",
            "yyyy年MM月dd日HH:mm",
            "yyyy年MM月dd日H时mm分",
            "yyyy年MM月dd日HH时m分",
            "yyyy年MM月dd日H时m分",
            "yyyy年M月dd日HH时mm分",
            "yyyy年M月dd日H时mm分",
            "yyyy年M月dd日HH时m分",
            "yyyy年M月dd日H时m分",
            "yyyy年MM月d日HH时mm分",
            "yyyy年MM月d日H时mm分",
            "yyyy年MM月d日HH时m分",
            "yyyy年MM月d日H时m分",
            "yyyy年M月d日HH时mm分",
            "yyyy年M月d日H时mm分",
            "yyyy年M月d日HH时m分",
            "yyyy年M月d日H时m分",
            "yyyy-MM-ddHH:mm:ss",
            "yyyy-MM-ddHH:mm",
            "yyyy-MM-ddHH:m",
            "yyyy-MM-ddH:mm",
            "yyyy-MM-ddH:m",
            "yyyy-M-ddHH:mm",
            "yyyy-M-ddHH:m",
            "yyyy-M-ddH:mm",
            "yyyy-M-ddH:m",
            "yyyy-MM-dHH:mm",
            "yyyy-MM-dHH:m",
            "yyyy-MM-dH:mm",
            "yyyy-MM-dH:m",
            "yyyy-M-dHH:mm",
            "yyyy-M-dHH:m",
            "yyyy-M-dH:mm",
            "yyyy-M-dH:m",
            "yyyy-MM-dd",
            "yyyy年MM月dd日H时",
            "yyyy/MM/ddHH:mm:ss",
            "yyyy/MM/ddHH:mm",
            "yyyy/MM/ddHH:m",
            "yyyy/MM/ddH:mm",
            "yyyy/MM/ddH:m",
            "yyyy/M/ddHH:mm",
            "yyyy/M/ddHH:m",
            "yyyy/M/ddH:mm",
            "yyyy/M/ddH:m",
            "yyyy/MM/dHH:mm",
            "yyyy/MM/dHH:m",
            "yyyy/MM/dH:mm",
            "yyyy/MM/dH:m",
            "yyyy/M/dHH:mm",
            "yyyy/M/dHH:m",
            "yyyy/M/dH:mm",
            "yyyy/M/dH:m",
            "yyyy/MM/dd",
            "yyyy.MM.dd",
            "yyyy-MM-ddHH:mm:ss.000",
            "yyyyMMddHH:mm",
            "yyyy-MM-dd'T'HH:mm:ss",
            "yyyy"
    };

    public static boolean isDate(String dttm, String format) {
        boolean retValue = false;
        if (dttm != null) {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            try {
                formatter.parse(dttm);
                retValue = true;
            } catch (ParseException e) {
            }
        }
        return retValue;
    }

}
