package digital.laboratory.platform.imr.component;


import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
 
/**
 * @Description:
 * @Author: fangzh
 * @CreateDate: 2020-11-18 17:53
 */
public class DateUtils {

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

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

    public static final String yyyy_MM = "yyyy-MM";
    
    public static final String TIME_ZONE = "GMT+8";

    /**
     * 获取当前的分钟
     *
     * @return
     */
    public static String getTime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
        String dateString = formatter.format(currentTime);
        String min = dateString.substring(14, 16);
        return min;
    }


    public static String formatDateByPattern(Date date,String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }


    public static String parseToCron(LocalDateTime taskTime) {
        //Date-->LocalDateTime
        Date date = toDate(taskTime);
        //规定cron表达式格式
        String cronFormat = "ss mm HH dd MM ? yyyy";

        return formatDateByPattern(date, cronFormat);
    }

    public static String getCron(Date date) {
        String dateFormat = "ss mm HH dd MM ? yyyy";
        return formatDateByPattern(date, dateFormat);
    }

    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }


    public static String localDateTimeToStr(LocalDateTime localDateTime){
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(yyyy_MM_dd_HH_mm_ss);

        String dateStr = localDateTime.format(fmt);

        return dateStr ;
    }

}
