1. 程式人生 > 實用技巧 >判斷兩個日期的跨度是否超過一年(12個月)

判斷兩個日期的跨度是否超過一年(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);
    }

}