Java程序获取两个日期之间的差异

标签 java arrays

这是程序:

class DateDiff {
int month[];//stores the number of days in all the months
int d,y,m,n;
DateDiff(int d,int m,int y)    {
    month=new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
    this.d=d;
    this.m=m;
    this.y=y;
    if(isLeap(y))
        month[1]=29;//if year is leap, February has 29 days
    else
        month[1]=28;
    n=0;
}
//function for checking for Leap Year
boolean isLeap(int y3)    {
    if((y3%400==0) || ((y3%100!=0)&&(y3%4==0)))
        return true;
    else
        return false;
}
//function for finding the number of days that have passed from start of the year 
int getDayNum(int d1,int m1,int y1)    {
    int i=0;
    if(isLeap(y1))//February leap year correction
        month[1]=29;
    else
        month[1]=28;
    for(;i<m1;i++)
        if(d<=month[i]&&i==m1-1)            {
            n+=d1;//add days when month hasn't completed
            break;
        }
        else 
            n+=month[i];  //add the normal number of days in the month
    return n;
}
//finding difference between the dates
int diff(int d2,int m2,int y2)    {
    int daysLeft=getDayNum(d,m,y);//store the number of days passed since start of the year y
    if(isLeap(y))
        daysLeft=366-daysLeft;//subtracting gives the number of days left in year y
    else
        daysLeft=365-daysLeft;
    for(int x=y+1;x<y2;x++)//for subsequent years add 366 or 365 days as required
        if(isLeap(x))
            daysLeft+=366;
        else
            daysLeft+=365;
    daysLeft+=getDayNum(d2,m2,y2);//add the number of days that have passed since start of year y2
    return daysLeft;}}
public class DateDifference{   
 public static int main(int d1,int m1,int y1,int d2,int m2,int y2)    {
        DateDiff obj=new DateDiff(d1,m1,y1);
        return obj.diff(d2,m2,y2);
    }
}

我没有通过该程序获得正确的输出。通过d1=20,m2=3,y1=1997d2=14,m2=2,y2=2015(日期为1997年3月20日和2015年2月14日) 。

正确的差异是 6540 天。我的程序给出了 6619 天。有人可以指出错误吗?

最佳答案

public DateDiff(int d,int m,int y){
    m--;

public int diff(int d2,int m2,int y2)    {
    m2--;

在某些地方,您必须将月份数字 1-12 的“人类”形式调整为您选择的内部形式 0-11。

根据您是否调整月份向所有通话添加公共(public)和私有(private)。

稍后更多修复

删除这些行。他们处于一个无用的地方。

if(isLeap(y))
    month[1]=29;//if year is leap, February has 29 days
else
    month[1]=28;
n=0;

不要使用字段作为本地总和。 (整数n)。

int getDayNum(int d1,int m1,int y1)    {
  if(isLeap(y1))//February leap year correction
    month[1]=29;
  else
    month[1]=28;
  int n = 0;
  for(int i = 0; i<m1; i++){
    n += month[i];
  }
  n += d1;//add days when month hasn't completed
  return n;
}

循环构造是不明智的。没有如果!

编码风格的一些改进:

class DateDiff
  int month[] = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
  int d,y,m;
  DateDiff(int d,int m,int y)    {
    this.d=d;
    this.m=m-1;
    this.y=y;
  }
  boolean isLeap(int y3)    {
    return (y3%400==0) || ((y3%100!=0)&&(y3%4==0));
  }
  int getDayNum(int d1,int m1,int y1){
    month[1] = isLeap(y1) ? 29 : 28;
    int n = 0;
    for(int i = 0; i < m1; i++){
      n += month[i];
    }
    n += d1;//add days when month hasn't completed
    return n;
  }
  int diff(int d2,int m2,int y2){
    m2--;
    int daysLeft = -getDayNum(d,m,y);
    for( int x = y; x<y2; x++ ){
      daysLeft += isLeap(x) ? 366 : 365;
    }
    daysLeft += getDayNum(d2,m2,y2);
    return daysLeft;
  }
}

关于Java程序获取两个日期之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28515103/

相关文章:

java - 如何按添加值的时间对TreeSet进行排序?

java - 使用 Libvirt Java API 连接到 Qemu

java - CDI JPA 通用 Dao

java - 其他方法的发送和接收单元

javascript - 如何比较输入和 JSON 数据

javascript - 如何使用 Firebase 和 JavaScript 将快照保存在数组中?

java - Jetty 无法启动 - ContextHandler 已停止

java - 如何将所有形状插入数组中?

Java:可附加字节数组

c - 在 C 编程中根据另一个字符串数组的单词从一个字符串数组中删除单词