使用java.util.Calendar返回间隔天数

java 代码
 
  1. **  
  2. * Calculates the number of days between two calendar days in a manner  
  3. * which is independent of the Calendar type used.  
  4. *  
  5. @param d1    The first date.  
  6. @param d2    The second date.  
  7. *  
  8. @return      The number of days between the two dates.  Zero is  
  9. *              returned if the dates are the same, one if the dates are  
  10. *              adjacent, etc.  The order of the dates  
  11. *              does not matter, the value returned is always >= 0.  
  12. *              If Calendar types of d1 and d2  
  13. *              are different, the result may not be accurate.  
  14. */  
  15. static int getDaysBetween (java.util.Calendar d1, java.util.Calendar d2) {  
  16.     if (d1.after(d2)) {  // swap dates so that d1 is start and d2 is end  
  17.         java.util.Calendar swap = d1;  
  18.         d1 = d2;  
  19.         d2 = swap;  
  20.     }  
  21.     int days = d2.get(java.util.Calendar.DAY_OF_YEAR) -  
  22.                d1.get(java.util.Calendar.DAY_OF_YEAR);  
  23.     int y2 = d2.get(java.util.Calendar.YEAR);  
  24.     if (d1.get(java.util.Calendar.YEAR) != y2) {  
  25.         d1 = (java.util.Calendar) d1.clone();  
  26.         do {  
  27.             days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);  
  28.             d1.add(java.util.Calendar.YEAR, 1);  
  29.         } while (d1.get(java.util.Calendar.YEAR) != y2);  
  30.     }  
  31.     return days;  
  32. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值