[JAVA]计算两个日期相差的天数
Posted yccy1230
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[JAVA]计算两个日期相差的天数相关的知识,希望对你有一定的参考价值。
算法的主要思路:
首先保证输入的年份要合法,即开始日期一定要小于结束日期
这里利用了两个辅助函数,一个是判断是否为闰年的isLeapYear;另一个是计算传入的某个日期在那一年的第几天theDayInTheYear(Date)
阐述一下计算的主要想法:
分为三种情况:
1.当输入的年份是同一年时,直接用计算出的两个日期所占这一年的天数差即可
2.当计算两个相邻年份时,计算(365(或366)-theDayInTheYear(beginDate))+theDayInTheYear(endDate)即可
3.当计算的两个日期相差大于至少一年时,只要按照2中的计算再加上相差年份的天数总和即可(注意相差年份中是闰年要加上366,平年加上365)
package class4_3;
public class Date
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;
package class4_3;
import java.util.Scanner;
public class DateCalc
private static int monthDay[] = 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ;
private static boolean isLeapYear(Date date)
// (辅助函数)判断是否为闰年
int year = date.getYear();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
private static boolean isLeapYear(int year)
// TODO Auto-generated method stub
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
private static int theDayInTheYear(Date date)
// (辅助函数)计算传入日期在这一年的第几天
int result = 0;
int day = date.getMonth();
if (isLeapYear(date))
monthDay[1] = 29;
else
monthDay[1] = 28;
for (int i = 0; i < (day - 1); i++)
result += monthDay[i];
return (result + day);
private static int calc(Date beiginDate, Date endDate)
// 计算日期差(非法计算返回-1,否则返回日期差)
// 确保日期可以计算(beginDate早于endDate)
if (beiginDate.getYear() > endDate.getYear()
|| beiginDate.getYear() == endDate.getYear() && theDayInTheYear(beiginDate) > theDayInTheYear(endDate))
return -1;
// 预先计算各自日期在当年的第几天
int begin = theDayInTheYear(beiginDate);
int end = theDayInTheYear(endDate);
// case 1:若为同一年
if (beiginDate.getYear() == endDate.getYear())
return (end - begin);
// case 2:若为相邻年份
else if ((beiginDate.getYear() + 1) == endDate.getYear())
int beginYearDays = 365;
if (isLeapYear(beiginDate))
beginYearDays = 366;
begin = beginYearDays - begin;
return (begin + end);
// case 3:相差一年以上
else
int beginYearDays = 365;
if (isLeapYear(beiginDate))
beginYearDays = 366;
begin = beginYearDays - begin;
int yearDay = 0;// 计算相隔年的天数和
for (int i = (beiginDate.getYear() + 1); i < endDate.getYear(); i++)
if (isLeapYear(i))
yearDay += 366;
else
yearDay += 365;
return (begin + end + yearDay);
public static void main(String[] args)
// 驱动函数
Date beginDate = new Date();
Date endDate = new Date();
Scanner in = new Scanner(System.in);
System.out.print("请输入起始年:");
beginDate.setYear(in.nextInt());
System.out.print("请输入起始月:");
beginDate.setMonth(in.nextInt());
System.out.print("请输入起始日:");
beginDate.setDay(in.nextInt());
System.out.print("请输入结束年:");
endDate.setYear(in.nextInt());
System.out.print("请输入结束月:");
endDate.setMonth(in.nextInt());
System.out.print("请输入结束日:");
endDate.setDay(in.nextInt());
int result = calc(beginDate, endDate);
if (result == -1)
System.out.println("您的输入有误!");
else
System.out.println("结果为:" + result);
以上是关于[JAVA]计算两个日期相差的天数的主要内容,如果未能解决你的问题,请参考以下文章