package com.rehome.jpahefengweather.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * @author LiuHuiYu
 * @version v1.0.0.0
 * Created DateTime 2022-01-06 15:35
 */
public class TimestampUtil {
    public static final String FORMAT_YEAR_MONTH_DAY = "yyyy-MM-dd";
    public static final String FORMAT_YEAR_MONTH_DAY_HOUR = "yyyy-MM-dd HH";
    public static final String FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND = "yyyy-MM-dd HH:mm:ss";

    /**
     * 转换成开始时间
     *
     * @param value 字符串
     * @return java.sql.Timestamp
     * @author LiuHuiYu
     * Created DateTime 2022-01-06 15:38
     */
    public static Timestamp beginTime(String value) {
        try {
            String tempV = value;
            if (tempV.length() == FORMAT_YEAR_MONTH_DAY.length()) {
                tempV += " 00:00:00";
            }
            else if (tempV.length() == FORMAT_YEAR_MONTH_DAY_HOUR.length()) {
                tempV += ":00:00";
            }
            else if (tempV.length() == FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE.length()) {
                tempV += ":00";
            }
            return Timestamp.valueOf(tempV);
        }
        catch (Exception e) {
            return null;
        }
    }

    /**
     * 转换成结束时间
     *
     * @param value 字符串
     * @return java.sql.Timestamp
     * @author LiuHuiYu
     * Created DateTime 2022-01-06 15:38
     */
    public static Timestamp endTime(String value) {
        try {
            String tempV = value;
            if (tempV.length() == FORMAT_YEAR_MONTH_DAY.length()) {
                tempV += " 23:59:59";
            }
            else if (tempV.length() == FORMAT_YEAR_MONTH_DAY_HOUR.length()) {
                tempV += ":59:59";
            }
            else if (tempV.length() == FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE.length()) {
                tempV += ":59";
            }
            return Timestamp.valueOf(tempV);
        }
        catch (Exception e) {
            return null;
        }
    }

    /**
     * 可以转换为 Timestamp 的字符串
     *
     * @param value 字符串
     * @return boolean
     * @author LiuHuiYu
     * Created DateTime 2022-01-06 15:37
     */
    public static boolean isTimestampString(String value) {
        try {
            String tempV = value;
            if (tempV.length() == FORMAT_YEAR_MONTH_DAY.length()) {
                tempV += " 23:59:59";
            }
            else if (tempV.length() == FORMAT_YEAR_MONTH_DAY_HOUR.length()) {
                tempV += ":59:59";
            }
            else if (tempV.length() == FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE.length()) {
                tempV += ":59";
            }
            Timestamp.valueOf(tempV);
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }

    public static String toString(Timestamp timestamp, String format) {
        try {
            return new SimpleDateFormat(format).format(timestamp);
        }
        catch (Exception ex) {
            return "";
        }
    }
}
