判断两个日期的跨度是否超过一年(12个月)

判断两个日期的跨度是否超过一年


写这个工具类是因为在处理查询业务的时候遇到了一个问题,
要求是查询的时间范围不能超过十二个月,自己找了一下没有比较简单的实现方法,
就自己理了一下思路自己写了这个工具类。

第一代产品

【思路】

  • 字符串 类型的日期 转换Date 类型
  • 再由 Date 类型 转换long 类型的毫秒值(这是Date记录时间的本质
  • 计算出一年的毫秒数,用两个参数时间相减的差进行比较
  • 如果总天数大于 365 天 或者 第二个参数早于第一个参数,则提示不合理输入
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 二师兄
 * @version V1.0
 * @Title: 时间校验工具类
 * @Description:
 * @date 2020/12/30 10:57
 */
public class DateFomart {
    public static void main(String[] args) {
        //规定需要转换的日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //文本转日期
        try {
            Date due = sdf.parse("2019-02-26");
            Date begin = sdf.parse("2018-02-26");
            long time1 = due.getTime();
            long time2 = begin.getTime();
            long time = time1 - time2;
            System.out.println(time + "=" + time1 + "-" + time2);
            if(time > 31536000000L || time < 0){
//                throw new BillException("查询时间期间不允许超过12个月。");
//                System.out.println("查询时间期间不允许超过12个月。");
                System.out.println("查询时间期间不合法");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

但是由于闰年的存在我们需要进行进一步判断,
如果存在跨越闰年 2 月 29 日,那么这一年就是 366 天
而不能单纯的按照 365 天来计算了。

第二代产品

【思路】

  • 相邻的两个年份最多会出现一个闰年
  • 开始时间是闰年且月份小于2,则在原来的毫秒值上加 1 天(一天是 86400000 毫秒
  • 或者结束时间是闰年且月份大于2,则加 1 天的毫秒值
    • 以上两条是判断是否存在闰年,是否跨过闰年的 2 月份
    • 最开始的思路不是这样的(写着写着发现这样写代码最少)

插播两行:(代码里用到了)
字符串类的常用方法:https://blog.csdn.net/weixin_44580492/article/details/106026843
时间日期常用工具类:https://blog.csdn.net/weixin_44580492/article/details/107367202

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

/**
 * @author 二师兄
 * @version V1.0
 * @Title: 时间校验工具类
 * @Description:
 * @date 2020/12/30 11:42
 */
public class DateCheckUtil {

    /**
     * 校验日期区间时间跨度是否在一年之内
     *      参数日期格式应为 yyyy-MM-dd,例如:2020-12-31
     * @param beginDate 开始日期
     * @param dueDate 结束日期
     */
    public static boolean checkIsOneYear(String beginDate, String dueDate){
        //365天的毫秒数
        long ms = 31536000000L;
        try {
            //规定需要转换的日期格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //文本转日期
            Date due = sdf.parse(dueDate);
            Date begin = sdf.parse(beginDate);
            long time = due.getTime() - begin.getTime();
            System.out.println(time + "=" + due.getTime() + "-" + begin.getTime());
            //天数大于366天或者小于一天一定属于不合理输入,返回false
            if(time > 31622400000L || time < 0L){
//                System.out.println("查询时间期间不合法。");
                return false;
            }

			//对字符串截取,取出年份和月份
            Integer beginYear = Integer.valueOf(beginDate.substring(0, 4));
            Integer beginMonth = Integer.valueOf(beginDate.substring(5, 7));
            Integer dueYear = Integer.valueOf(dueDate.substring(0, 4));
            Integer dueMonth = Integer.valueOf(dueDate.substring(5, 7));

            //判断是否为闰年,并跨过2月,如果是则增加一天的毫秒数
            if(isLeapYear(beginYear) && beginMonth <= 2){
                ms += 86400000;
            }else if(isLeapYear(dueYear) && dueMonth >= 2){
                ms += 86400000;
            }

            return time <= ms;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 给定一个年份,判断是否为闰年
     * @param year
     * @return
     */
    public static boolean isLeapYear(Integer year){
        if(year % 100 == 0){
            if(year % 400 == 0){
                return true;
            }
        }else if(year % 4 == 0){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String begin = "2020-01-15";
        String dueDate = "2021-01-15";
        boolean b = checkIsOneYear(begin, dueDate);
        System.out.println(b);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值