LocalDateUtils

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

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.aapoint.util; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; public class LocalDateTimeUtil { /** * 比较 localDateTime2 是否在localDateTime1之前(比较大小) * @param localDateTime1 * @param localDateTime2 * @return */ public static Boolean compare(LocalDateTime localDateTime1,LocalDateTime localDateTime2){ return localDateTime1.isBefore(localDateTime2); } /** * 获取当前月份前/后的月份的第一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String firstDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的第一天 String firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("第一天为:"+firstDay); return firstDay; } /** * 获取当前月份前/后的月份的最后一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String lastDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的最后一天 String lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("最后一天为:"+lastDay); return lastDay; } /** * 获取当时间前/后的时间(天) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,1,i); //获取天 String day = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("获取的时间为(天):"+day); return day; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainHours(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,2,i); //获取该月份的最后一天 String hours = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(小时):"+hours); return hours; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainMinutes(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,3,i); //获取该月份的最后一天 String minutes = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(分钟):"+minutes); return minutes; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainSeconds(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,4,i); //获取该月份的最后一天 String seconds = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(秒):"+seconds); return seconds; } public static void main(String[] args) { System.out.println("当前时间为:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); System.out.println("前一个月份的第一天为:"+LocalDateTimeUtil.firstDay(1,1)); System.out.println("前一个月份的最后一天为:"+LocalDateTimeUtil.lastDay(1,1)); System.out.println("当前时间的前一天为:"+LocalDateTimeUtil.obtainDay(1,1)); System.out.println("当前时间的后一天为:"+LocalDateTimeUtil.obtainDay(2,1)); System.out.println("当前时间的前一小时为:"+LocalDateTimeUtil.obtainHours(1,1)); System.out.println("当前时间的后一小时为:"+LocalDateTimeUtil.obtainHours(2,1)); System.out.println("当前时间的前一分钟为:"+LocalDateTimeUtil.obtainMinutes(1,1)); System.out.println("当前时间的后一分钟为:"+LocalDateTimeUtil.obtainMinutes(2,1)); System.out.println("当前时间的前一秒为:"+LocalDateTimeUtil.obtainSeconds(1,1)); System.out.println("当前时间的后一秒为:"+LocalDateTimeUtil.obtainSeconds(2,1)); } private static LocalDateTime getLocalDateTime(Integer state,Integer type,Integer i) { LocalDateTime date; if(state == 0){ date = LocalDateTime.now(); }else if(state == 1){ if(type == 0) { //获取月 date = LocalDateTime.now().minusMonths(i); }else if(type == 1){ //获取天 date = LocalDateTime.now().minusDays(i); }else if(type == 2){ //获取小时 date = LocalDateTime.now().minusHours(i); }else if(type == 3){ //获取分钟 date = LocalDateTime.now().minusMinutes(i);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值