package com.sunyard.ssp.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

public class DateUtil {

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * String 型转 date
     * @param stringDate
     * @return
     */
    public static LocalDate StringToDate(String stringDate){
        LocalDate localDate = LocalDate.now();
        try {
            if (stringDate != null && stringDate != "") {
                localDate = LocalDate.parse(stringDate);
            }
            return localDate;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 时间转String 遇到null 直接返回null
     * @param stringDate
     * @return
     */
    public static LocalDate StringToDateOrNull(String stringDate){
        try {
            if (stringDate != null && stringDate != "") {
                LocalDate  localDate = LocalDate.parse(stringDate);
                return localDate;
            }else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获得一个格式化时间
     * @return
     */
    public static String getFormatDate(){
        Date localDateTime = new Date();
        String result = simpleDateFormat.format(localDateTime);
        return result;
    }


}
