java 计算当前日期所在周的周一是那一天

这是一个Java工具类,用于处理LocalDate对象,提供计算当前日期所在周的周一、月底、上一周的开始和结束日期等方法。例如,获取指定日期的上一周开始日期、上一月的同一天等。适用于日期相关的计算场景。
摘要由CSDN通过智能技术生成

分享工具类:各种日期计算(注:工具类中的周的开始时间是周日

计算当前日期所在周的周一是那一天

计算当前日期所在月份的第一天

。。。。。。。



import java.time.LocalDate;

/**
 * 日期计算辅助类
 * @author Lim
 * @date 2019/11/10
 */
public class LocalDateUtil {

    /**
     * 周开始日期,周日为周开始时间
     * @param date 指定日期
     * @return LocalDate
     *  eg: 2019-11-10  -> 2019-11-10
     */
    public static LocalDate getStartOfWeek(LocalDate date) {
        return date.minusDays(date.getDayOfWeek().getValue() % 7);
    }

    /**
     * 周结束日期,周日为周开始时间
     * @param date 指定日期
     * @return LocalDate
     *  eg: 2019-11-10  -> 2019-11-16
     */
    public static LocalDate getEndOfWeek(LocalDate date) {
        return date.plusDays(7 - 1 - date.getDayOfWeek().getValue() % 7);
    }

    /**
     * 上一周的开始时间
     * @param date 指定日期
     * @return LocalDate
     *  eg: 2019-12-18 -> 2019-12-08
     */
    public static LocalDate getStartOfBeforeWeek(LocalDate date) {
        date = date.minusDays(7);
        return getStartOfWeek(date);
    }

    /**
     * 上一周的同一天
     * @param date 指定日期
     * @return LocalDate
     *   eg: 2019-12-18 -> 2019-12-11
     */
    public static LocalDate getBeforeWeek(LocalDate date) {
        return date.minusDays(7);
    }

    /**
     * 月开始日期
     * @param date 指定日期
     * @return LocalDate
     *  eg: 2019-11-10  -> 2019-11-16
     */
    public static LocalDate getStartOfMonth(LocalDate date) {
        return date.withDayOfMonth(1);
    }

    /**
     * 月结束日期
     * @param date 指定日期
     * @return LocalDate
     *  eg: 2019-11-10  -> 2019-11-30
     */
    public static LocalDate getEndOfMonth(LocalDate date) {
        return date.withDayOfMonth(date.lengthOfMonth());
    }

    /**
     * 上一月开始日期
     * @param date 指定日期
     * @return LocalDate
     *  eg: 2019-12-18 -> 2019-11-01
     */
    public static LocalDate getStartOfBeforeMonth(LocalDate date) {
        // 先定位到指定月1日,后减15天至上一月,在查询上一月1号
        date = date.withDayOfMonth(1);
        date = date.minusDays(15);
        return getStartOfMonth(date);
    }

    /**
     * 上一月的同一天
     * @param date 指定日期
     * @return LocalDate
     *   eg: 2019-12-18 -> 2019-11-18
     */
    public static LocalDate getBeforeMonth(LocalDate date) {
        LocalDate lastMonth = getStartOfBeforeMonth(date);

        int lengthOfMonth = date.lengthOfMonth();
        int lengthOfBeforeMonth = lastMonth.lengthOfMonth();

        int day = date.getDayOfMonth();

        if(day == lengthOfMonth || day >=  lengthOfBeforeMonth) {
            // 指定月最后一天 或 大于上一月总天数
            lastMonth = lastMonth.withDayOfMonth(lengthOfBeforeMonth);
        } else {
            lastMonth = lastMonth.withDayOfMonth(day);
        }

        return lastMonth;
    }

    public static void main(String[] args) {
        System.out.println(getStartOfBeforeWeek(LocalDate.parse("2019-11-29")));
        System.out.println(getStartOfBeforeWeek(LocalDate.parse("2019-11-30")));
        System.out.println(getStartOfBeforeWeek(LocalDate.parse("2020-01-01")));
        System.out.println(getBeforeWeek(LocalDate.parse("2020-01-02")));
        System.out.println(getBeforeWeek(LocalDate.parse("2020-01-30")));
        System.out.println(getBeforeWeek(LocalDate.parse("2020-01-31")));
    }

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ivan_梦方舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值