用Java实现计算两个日期之间相差的天数

public Calculate {
	public static void main(String[] args){
		int days = getBetweenDays(2019,5,20,2020,2,14);
	}
	//计算相差天数
	public static int getBetweenDays(int fromYear,int fromMonth,int fromDay,int toYear,int toMonth,int toDay){
		int fromdays = getDays(fromYear,fromMonth,fromDay);
		int todays = getDays(toYear,toMonth,toDay)
		int days = 0;
		for(int y = fromYear; y < toYear; y++){
			days += (check(y) ? 366 : 365);
		}
		return days - fromdays + todays;
	}
	//计算当前日期是这一年的第几天
	public static int getDays(int year,int month,int day){
		int days = day;
		for (int m = 1; m <= month; m++){
			if(m == 2 || m == 4 || m == 6 || m == 9 || m == 11){
				days += 30;
			}else if(m == 2){
				days =+ (check(y) ? 29 : 28);
			}else{
				days += 31;
			}
		}
		return days;
	}
	//判断闰年,是闰年返回true,否则返回false
	public static boolean check(int year){
		return year % 4 == 0 && year % 100 == 0 || year % 400 == 0;
	}
}

你可能感兴趣的:(用Java实现计算两个日期之间相差的天数)