初学Android,遇到这个问题还不知道怎么办,Android没有提供函数来计算,而且自己数据库用的是MySQL,也是自己不熟悉的数据库,就没有过多的想用SQL来写。
在网上找了几个,感觉有些复杂,想了想有一点思路,写出来可以实现
思路:
- 首先判断是否日期一前一后
- 边计数边增加比较早的日期
- 等到两个日期一样时,停止操作,返回计数
/**
* Created by Harry on 2015/4/27.
* This class is used for compute days between two different dates
*/
public class DateCompute {
private static String TAG = DateCompute.class.getName();
private static Calendar startDate;
private static Calendar endDate;
private static int dayCount;
public DateCompute(Calendar p_startDate, Calendar p_endDate) {
startDate = p_startDate;
endDate = p_endDate;
dayCount = 0;
}
/**
* Since both are Calendar, there is no need to add one more month each.
* @return
*/
public int computeCalendar(){
/*if startDate is before end