计算两日期相差的天数

计算两日期相差的天数

  1 public static void main(String[] args) throws ParseException {
  2 
  3         SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss" );
  4         String beginStr1 = "2021/11/24 10:50:13";   // 开始时间1
  5         String beginStr2 = "2022/04/11 10:50:15";   // 开始时间2,注意时间中的时分秒对getAlmostDayRangeWithoutDay方法的影响
  6 
  7         Date begin=sdf.parse(beginStr1);
  8         Date end=sdf.parse(beginStr2);
  9 
 10         System.out.println(getAlmostDayRangeWithoutDay(begin, end));
 11 
 12         System.out.println(getDayRange(begin,end));
 13 }
 14 
 15  /**
 16      * 帮助方法:{0,日期1}与{1,日期2}相差的天数(不足一天舍去)
 17      *
 18      * @param beginDate 开始日期
 19      * @param endDate   结束日期
 20      * @return 两个日期间相差的天数(不足一天舍去)
 21      */
 22     public static int getAlmostDayRangeWithoutDay(Date beginDate, Date endDate) {
 23         if (beginDate == null || endDate == null) {
 24             return 0;
 25         }
 26         boolean isNegative = false;
 27         if (beginDate.after(endDate)) {
 28             Date temp = beginDate;
 29             beginDate = endDate;
 30             endDate = temp;
 31             isNegative = true;
 32         }
 33 
 34         Calendar calenderBegin = Calendar.getInstance();
 35         Calendar calenderEnd = Calendar.getInstance();
 36 
 37         calenderBegin.setTime(beginDate);
 38         calenderEnd.setTime(endDate);
 39 
 40         calenderBegin.set(Calendar.MILLISECOND, 0);
 41         calenderEnd.set(Calendar.MILLISECOND, 0);
 42 
 43         if (!calenderBegin.before(calenderEnd) && !calenderBegin.after(calenderEnd)) {
 44             return 0;
 45         }
 46 
 47         int dayRange = 0;
 48         while (calenderBegin.before(calenderEnd)) { //日期+1,day_of_month效果一样
 49             calenderBegin.add(Calendar.DAY_OF_MONTH, 1);
 50             dayRange += 1;
 51         }
 52 
 53         if (calenderBegin.after(calenderEnd)) {  //不足一天减去一天
 54             dayRange -= 1;
 55         }
 56 
 57 
 58         if (isNegative) {
 59             dayRange = dayRange * -1;
 60         }
 61 
 62         return dayRange;
 63     }
 64 
 65     /**
 66      * 帮助方法:{0,日期1}与{1,日期2}相差的天数(忽略小时)
 67      *
 68      * @param beginDate1 开始日期
 69      * @param endDate1   结束日期
 70      * @return 两个日期间相差的天数(忽略小时)
 71      */
 72     public static int getDayRange(Date beginDate1, Date endDate1) {
 73 
 74         SimpleDateFormat sdf =new SimpleDateFormat("yyyy/MM/dd" );
 75         Date beginDate =null;
 76         Date endDate =null;
 77         try {
 78             beginDate = sdf.parse(sdf.format(beginDate1));
 79             endDate = sdf.parse(sdf.format(endDate1));
 80         } catch (ParseException e) {
 81             e.printStackTrace();
 82         }
 83 
 84         if (beginDate == null || endDate == null) {
 85             return 0;
 86         }
 87         boolean isNegative = false;
 88 
 89         if (beginDate.after(endDate)) {
 90             Date temp = beginDate;
 91             beginDate = endDate;
 92             endDate = temp;
 93             isNegative = true;
 94         }
 95         Calendar calenderBegin = Calendar.getInstance();
 96         Calendar calenderEnd = Calendar.getInstance();
 97         calenderBegin.setTime(beginDate);
 98         calenderEnd.setTime(endDate);
 99 
100         int dayRange = 0;
101         while (calenderBegin.getTime().before(calenderEnd.getTime())) {
102             calenderBegin.add(Calendar.DAY_OF_MONTH, 1);
103             dayRange += 1;
104         }
105 
106         System.out.println(calenderBegin.getTime());
107         if (isNegative) {
108             dayRange = dayRange * -1;
109         }
110 
111         return dayRange;
112     }

Java类计算两日期之间相差的天数,方法一不满一天舍去;方法二忽略对小时的计算,仅计算日期

输出:

138
137

原文地址:https://www.cnblogs.com/xnuuuu/archive/2022/05/24/16305086.html