各种日期格式转换工具类

各种日期格式转换工具类


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();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值