如何获得两个日历实例之间的天数? [英] How to get number of days between two calendar instance?

查看:160
本文介绍了如何获得两个日历实例之间的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到的天数两个日历对象之间的区别,如果有时间的变化就像如果从23时钟滴答:59-0:00应该有每天差别。

I want to find the difference between two Calendar objects in number of days if there is date change like If clock ticked from 23:59-0:00 there should be a day difference.

我写这个

public static int daysBetween(Calendar startDate, Calendar endDate) {  
    return Math.abs(startDate.get(Calendar.DAY_OF_MONTH)-endDate.get(Calendar.DAY_OF_MONTH));  
} 

但它不是因为它的工作只给天之间的区别,如果有区别月份其毫无价值。

but its not working as it only gives difference between days if there is month difference its worthless.

推荐答案

请尝试以下方法:

public static long daysBetween(Calendar startDate, Calendar endDate) {
    long end = endDate.getTimeInMillis();
    long start = startDate.getTimeInMillis();
    return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start));
}

这篇关于如何获得两个日历实例之间的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆