天天看點

各種日期格式轉換工具類

各種日期格式轉換工具類

import lombok.extern.slf4j.Slf4j;

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

/**
 * 各種日期格式轉換工具類
 */
@Slf4j
public class DateUtil {

    /**
     * 格式 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getBuyTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設定日期格式
        return df.format(new Date());
    }

    /**
     * 得到今日日期 格式 yyyyMMdd
     *
     * @return
     */
    public static String getTodayDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//設定日期格式
        return df.format(new Date());
    }

    /**
     * 格式 yyyy-MM-dd
     *
     * @return
     */
    public static String getSendDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//設定日期格式
        return df.format(new Date());
    }


    /**
     * @param year  年 yyyy
     * @param month 月 1-12
     * @return 當年當月有多少天
     */
    public static int getMonthDays(int year, int month) {
        int days = 0;
        boolean isLeapYear = false;
        //閏年的條件(滿足之一即可):(1)能被4整除,但不能被100整除;(2)能被400整除
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) isLeapYear = true;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 2:
                if (isLeapYear) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            default:
                log.debug("month比對錯誤!");
                break;
        }
        return days;
    }

    /**
     * @param s 傳入的資料,格式MM/dd string 01-31
     * @return 返還的格式 int 1-31
     */
    public static int getStringFormatToInt(String s) {
        int i = Integer.parseInt(s.substring(0, 1));
        int format = 0;
        if (i == 0) {// 01-09
            String sub = s.substring(1);
            format = Integer.parseInt(sub);
        } else {// 10...
            format = Integer.parseInt(s);
        }
        return format;
    }

    /**
     * 計算每日,要發送的日期範圍
     *
     * @param year         年 yyyy
     * @param month        月 MM
     * @param calculateDay 計算的日 dd
     * @return year + "-" + mm + "-" + dd + " 15:00:00";
     */
    public static String getCalculateTime(int year, int month, int calculateDay) {
        // day<=0,說明要向前一個月借位
        if (calculateDay <= 0) {
            month = month - 1;
            // month==0,跨年借
            if (month == 0) {
                month = 12;
                year = year - 1;
            }
            int monthDays = DateUtil.getMonthDays(year, month);
            calculateDay += monthDays;
        }
        String dd = "";
        String mm = "";
        if (month < 10) mm = "0" + month;
        if (calculateDay < 10) dd = "0" + calculateDay;
        return year + "-" + mm + "-" + dd + " 15:00:00";
    }

    /**
     * @return 得到今天是周幾
     * @throws Throwable
     */
    public static int getDayForWeek() {
        // 擷取今天的日期,格式為yyyy-MM-dd形式
        LocalDate now = LocalDate.now();
        // 得到今天是周幾,從1(星期一)到7(星期日)。
        return now .getDayOfWeek().getValue();
    }

    /**
     * @return 得到今天是幾号
     */
    public static int getDayForNumber() {
        // 擷取今天的日期
        LocalDate now = LocalDate.now();
        // 今天是幾号
        return now .getDayOfMonth();
    }

}