LocalDateUtils

本文介绍了一个名为LocalDateUtils的工具类,用于解析日期字符串、转换日期格式,并提供判断时间差是否在特定范围内如年、天、周的功能。通过示例展示了如何使用这些方法进行日期操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.tuya;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * 本地日期工具类
 */
public class LocalDateUtils {

    public static final String PATTEN_DATE_SPLIT = "yyyy-MM-dd";
    public static final String PATTEN_DATE = "yyyyMMdd";

    static class DateException extends RuntimeException {
        public DateException(String message) {
            super(message);
        }

        public DateException(String message, Throwable cause) {
            super(message, cause);
        }
    }

    /**
     * 解析日期字符串为日期
     *
     * @param specifiedDate 待解析的日期
     * @param pattern       解析表达式
     */
    public static LocalDate parseLocalDate(String specifiedDate, String pattern) {
        if (StringUtils.isBlank(specifiedDate)) {
            throw new DateException("The date cannot be empty");
        }
        try {
            return LocalDate.parse(specifiedDate, getDateTimeFormatter(pattern));
        } catch (Exception e) {
            throw new DateException("The date is illegal");
        }
    }

    /**
     * 获取日期格式格式化实例
     *
     * @param pattern 表达式
     * @return java.time.format.DateTimeFormatter
     */
    private static DateTimeFormatter getDateTimeFormatter(String pattern) {
        pattern = StringUtils.isNotBlank(pattern) ? pattern : PATTEN_DATE_SPLIT;
        return DateTimeFormatter.ofPattern(pattern);
    }

    /**
     * 判断时间差是否小于n年
     * 例如:判断开始日期和结束日期是否在1年之内
     *
     * @param startDate
     * @param endDate
     * @param pattern
     * @param numberOfYears
     * @return
     */
    public static boolean judgeTimeDifferenceLessThanNYears(String startDate, String endDate, String pattern, int numberOfYears) {
        return judgeChronDiffLessThan(startDate, endDate, pattern, ChronoUnit.YEARS, numberOfYears);
    }

    /**
     * 小于n天,返回true
     *
     * @param startDate
     * @param endDate
     * @param pattern
     * @param numberOfDays
     * @return
     */
    public static boolean judgeDaysDiffLessThanNDays(String startDate, String endDate, String pattern, int numberOfDays) {
        return judgeChronDiffLessThan(startDate, endDate, pattern, ChronoUnit.DAYS, numberOfDays);
    }

    public static boolean judgeWeeksDiffLessThanNWeeks(String startDate, String endDate, String pattern, int numberOfDays) {
        return judgeChronDiffLessThan(startDate, endDate, pattern, ChronoUnit.WEEKS, numberOfDays);
    }

    /**
     * 判断时间差是否在预期范围内
     *
     * @param startDate
     * @param endDate
     * @param pattern
     * @param unit
     * @param numberOfUnit
     * @return
     */
    public static boolean judgeChronDiffLessThan(String startDate, String endDate, String pattern, ChronoUnit unit, int numberOfUnit) {
        LocalDate startLocalDate = parseLocalDate(startDate, pattern);
        LocalDate endLocalDate = parseLocalDate(endDate, pattern);
        if (!(startLocalDate.isBefore(endLocalDate) || startLocalDate.isEqual(endLocalDate))) {
            throw new DateException("The start date must be earlier than or equal to the end date");
        }
        LocalDate newDate = startLocalDate.plus(numberOfUnit, unit);
        /**
         * 新日期与结束日期相同 或 新日期仍然大于结束日期:开始日期和结束日期在预期范围差内
         */
        if (newDate.isAfter(endLocalDate)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        testDiffYears();
        testDiffDays();
        testDiffWeeks();
    }

    private static void testDiffYears() {
        String startDate = "2021-01-01";
        String endDate = "2022-01-01";
        int numberOfYears = 1;
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s年内结果:%s", startDate, endDate, numberOfYears,
                judgeTimeDifferenceLessThanNYears(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));

        startDate = "2000-01-01";
        endDate = "2000-12-31";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s年内结果:%s", startDate, endDate, numberOfYears,
                judgeTimeDifferenceLessThanNYears(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));

        startDate = "2000-01-02";
        endDate = "2000-12-31";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s年内结果:%s", startDate, endDate, numberOfYears,
                judgeTimeDifferenceLessThanNYears(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));

        startDate = "2000-01-01";
        endDate = "2001-01-01";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s年内结果:%s", startDate, endDate, numberOfYears,
                judgeTimeDifferenceLessThanNYears(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));

        startDate = "2000-01-01";
        endDate = "2000-12-29";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s年内结果:%s", startDate, endDate, numberOfYears,
                judgeTimeDifferenceLessThanNYears(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));
    }

    private static void testDiffDays() {
        String startDate = "2021-01-01";
        String endDate = "2022-01-01";
        int numberOfYears = 365;
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s天内结果:%s", startDate, endDate, numberOfYears,
                judgeDaysDiffLessThanNDays(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));

        startDate = "2000-01-01";
        endDate = "2000-12-31";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s天内结果:%s", startDate, endDate, numberOfYears,
                judgeDaysDiffLessThanNDays(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));

        startDate = "2000-01-02";
        endDate = "2000-12-31";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s天内结果:%s", startDate, endDate, numberOfYears,
                judgeDaysDiffLessThanNDays(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));

        startDate = "2000-01-01";
        endDate = "2001-01-01";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s天内结果:%s", startDate, endDate, numberOfYears,
                judgeDaysDiffLessThanNDays(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));

        startDate = "2000-01-01";
        endDate = "2000-12-29";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s天内结果:%s", startDate, endDate, numberOfYears,
                judgeDaysDiffLessThanNDays(startDate, endDate, PATTEN_DATE_SPLIT, numberOfYears)));
    }

    private static void testDiffWeeks() {
        String startDate = "2021-01-01";
        String endDate = "2022-01-01";
        int numberOfWeeks = 52;
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s周内结果:%s", startDate, endDate, numberOfWeeks,
                judgeWeeksDiffLessThanNWeeks(startDate, endDate, PATTEN_DATE_SPLIT, numberOfWeeks)));

        startDate = "2000-01-01";
        endDate = "2000-12-31";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s周内结果:%s", startDate, endDate, numberOfWeeks,
                judgeWeeksDiffLessThanNWeeks(startDate, endDate, PATTEN_DATE_SPLIT, numberOfWeeks)));

        startDate = "2000-01-02";
        endDate = "2000-12-31";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s周内结果:%s", startDate, endDate, numberOfWeeks,
                judgeWeeksDiffLessThanNWeeks(startDate, endDate, PATTEN_DATE_SPLIT, numberOfWeeks)));

        startDate = "2000-01-01";
        endDate = "2001-01-01";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s周内结果:%s", startDate, endDate, numberOfWeeks,
                judgeWeeksDiffLessThanNWeeks(startDate, endDate, PATTEN_DATE_SPLIT, numberOfWeeks)));

        startDate = "2000-01-01";
        endDate = "2000-12-29";
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s周内结果:%s", startDate, endDate, numberOfWeeks,
                judgeWeeksDiffLessThanNWeeks(startDate, endDate, PATTEN_DATE_SPLIT, numberOfWeeks)));

        startDate = "2021-12-01";
        endDate = "2021-12-29";
        numberOfWeeks = 4;
        System.out.println(String.format("开始日期:%s,结束日期:%s,相差范围在%s周内结果:%s", startDate, endDate, numberOfWeeks,
                judgeWeeksDiffLessThanNWeeks(startDate, endDate, PATTEN_DATE_SPLIT, numberOfWeeks)));
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值