获取两个日期之间的月份数与天数

 /**
     * 返回一个二维数组,单位分别是月和日,代表两个Date之差。 <br>
     * 本方法忽略小时和分钟。 <br>
     * <br>
     * 例: <br>
     * 1,2012年6月1日到2012年6月3日,返回值是[0,2] (2天) <br>
     * 2,2012年6月15日到2012年8月4日,返回值是[1,20] (1个月零20天) <br>
     * 3,2011年11月3日到2013年1月14日,返回值是[14,11] (14个月零11天)
     * 
     * @param olderDate
     *            较早的日期
     * @param newerDate
     *            较晚的日期
     */

public static int[] getDateDifferenceInMonthAndDay(Date olderDate, Date newerDate)
            throws IllegalAugumentException
    {
        int[] differenceInMonthAndDay = new int[2];
        int years = 0;
        int months = 0;
        int days = 0;

        Calendar older = Calendar.getInstance();
		Calendar newer = Calendar.getInstance();
		older.setTime(olderDate);
		newer.setTime(newerDate);
		
        if(olderDate.getTime()>newerDate.getTime()){
        	throw new IllegalAugumentException();
        }else{
        	years = newer.get(Calendar.YEAR)-older.get(Calendar.YEAR);
        	if(years>=0){
        		
        		months = newer.get(Calendar.MONTH)-older.get(Calendar.MONTH)+12*years;
            	older.add(Calendar.MONTH,months);
            	days = newer.get(Calendar.DATE)-older.get(Calendar.DATE);
            	
            	if(days<0){
            		months = months - 1;
            		older.add(Calendar.MONTH,-1);
            	}
            	
            	days = daysBetween(newer.getTime(),older.getTime());
            	differenceInMonthAndDay[0] = months;
            	differenceInMonthAndDay[1] = days;
        	}
        	
        }  
        return differenceInMonthAndDay;
    }

   /**
     * 取两个Date之间的天数差<br>
     * <br>例:newerDate是6月3日,olderDate是5月31日,则应返回3
     * <br>一个更极端的列子:newerDate是6月3日 00:01,olderDate是6月2日 23:59,则应返回1,说明相差一天,即便实际上只差2分钟
     * <br>此代码来自网上
     * <br>http://blog.csdn.net/rmartin/article/details/1452867
     * @param newerDate
     * @param olderDate
     * @return
     */
    public static int daysBetween(Date newerDate, Date olderDate)
    {
        Calendar cNow = Calendar.getInstance();
        Calendar cReturnDate = Calendar.getInstance();
        cNow.setTime(newerDate);
        cReturnDate.setTime(olderDate);
        setTimeToMidnight(cNow);
        setTimeToMidnight(cReturnDate);
        long todayMs = cNow.getTimeInMillis();
        long returnMs = cReturnDate.getTimeInMillis();
        long intervalMs = todayMs - returnMs;
        return millisecondsToDays(intervalMs);
    }

    private static int millisecondsToDays(long intervalMs)
    {
        return (int) (intervalMs / (1000 * 86400));
    }

    private static void setTimeToMidnight(Calendar calendar)
    {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值