计算任意两个日期之间的天数【附源码】
题目:计算两个日期之间的天数
思路:
- 同年同月:之间两个天数相减即可,即(day1-day2-1)很简单。
- 同年不同月:先计算完整月数的天数,即从指定日期的写个月开始计算,计算每个月有多少天,都加起来即可;然后计算指定日期到月初和月末的天数。如2019-6-12和2019-2-3,我们先计算3、4、5这三个完整月份的天数,然后计算6月12日到月初的天数,再计算2月3日到月末的天数,三者加起来就是两日期之间的天数。
- 年月日都不同:思路和计算不同月的类似。我们可以先计算两个年份之间完整年份的天数,然后再计算较大年份距离年初(1月1日)的天数,再计算较小年份距离年末(12-31)的天数,将三者加起来就是两个日期之间的天数。
下面帖出我自己的代码:
public class HowManyDays {
private int year;
private int month;
private int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public HowManyDays(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public HowManyDays() {}
/*
* 判断某年是否是闰年
*/
public boolean isLeapYear(int year) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
return true;
}
return false;
}
/*
* 某年某月 月有多少天
*/
public int daysOfMonth(int year, int month) {
if(month>12||month<=0) {
System.out.println("日期不合法");
return -1;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
return 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else if (isLeapYear(year) && month == 2) {
return 29;
} else {// 非闰年二月28天
return 28;
}
}
// 计算机本年一共多少天
public int daysOfYear(int year) {
int result = 0;
result = 7 * 31 + 4 * 30;// 不包括闰月
if (isLeapYear(year)) {
result += 29;
} else {
result += 28;
}
return result;
}
/*
* 计算指定日期到年初的天数
*/
public int daysOfYearThis(HowManyDays date) {
int year=date.getYear();
int month=date.getMonth();
int day=date.getDay();
int result = 0;
int month2 = 1;// 该年的元月
if(year<0||month>12||month<0||day>31||day<0) {
System.out.println("日期不合法");
return -1;
}
while (month != month2) {
result += daysOfMonth(year, month2);
month2++;
}
result = result + day;
return result;
}
/*
* 计算指定日期到年末的天数
* 思路:先算完整的月份,即从当前日期的下一个月算起,直到下一年的1月1日,此时算出了下一个月到1月1日之间的天数
* 再加上此时日期到月底的天数,从而得到本日期到年末之间的天数
*/
public int daysOfYear_Next(HowManyDays date) {
int year=date.getYear();
int month=date.getMonth();
int day=date.getDay();
int result = 0;
int month2 = 1;// 下一年的元月
int m = month;
if(year<0||month<0||month>12||day>31||day<0) {
System.out.println("日期不合法");
return -1;
}
while (month2 != m) {
m++;// 先计算下一月到次年1月1日的天数
if (m < 12) {
result += daysOfMonth(year, m);
} else if (m == 12) {//年内最后一个月
result += daysOfMonth(year, m);//计算12月份天数
result += daysOfMonth(year, month) - day;//daysOfMonth(year, month) - day计算指定日期到月末天数
m = 1;//循环结束
}
}
return result;
}
/*
* date1>date2
* */
public int how_many_day(HowManyDays date1,HowManyDays date2) {
int result = 0;
int m1, m2, y1, y2;
//exchange(date1, date2);
//System.out.println(date1.getYear()+""+date1.getMonth()+" "+date1.getDay());
m1 = date1.getMonth();
m2 = date2.getMonth();
y1 = date1.getYear();
y2 = date2.getYear();
if (date1.getYear() == date2.getYear() && date1.getMonth() == date2.getMonth()) {// 同年同月
result = Math.abs(date1.getDay() - date2.getDay());//直接计算日期天数之间的差值
} else if (date1.getYear() == date2.getDay()) {// 同年不同月
while ((m2 + 1) != m1) {
result += daysOfMonth(date2.getYear(), m2);
m2++;
}
/*
* daysOfMonth(year2, month2)-day2-1 计算较小月距离月末还有多少天
*/
result = result + date1.getDay() + daysOfMonth(date2.getYear(), date2.getMonth()) - date2.getDay() - 1;
} else {// 不同年,不同月
while (y1 != (y2+1)) {//计算完整年的所有天数
result += daysOfYear(y2);
y2++;
}
/*
* 在所有完整年计算完之后,再加上较大年份距离年初的天数和较小年份到年初的天数
* */
result += daysOfYearThis(date1) + daysOfYear_Next(date2);
}
return result;
}
public static void main(String ags[]) {
HowManyDays hd = new HowManyDays();
HowManyDays date1=new HowManyDays(2019,10,13);
HowManyDays date2=new HowManyDays(2017, 10, 12);
int days = hd.how_many_day(date1,date2);
System.out.println(days);
}
}