daysApart计算两次数字

时间:2014-10-29 01:48:18

标签: java

我试图编写一个程序来查找两个日期之间的天数。我不知道造成错误的原因是什么,但模式看起来好像是在一年中的第一个日期和同一年的第二个日期之间添加了天数。这是一个不好的解释,但请查看daysApart方法(main之前的最后一个方法)。 这是它遇到的错误的打印输出: http://i.imgur.com/yYTKHW9.png

public class PA05{


    public static boolean isLeapYear(int y){
        if ((y%4 ==0) && (y%100 != 0)){
            return true;
        } else if ((y%4==0) && (y%100==0) && (y%400 == 0)) {
            return true;
        } else{
            return false;
        }
    }

        public static int daysInMonth(int month, int year){
            if (month == 1){
                return 31;
            }
            if((month == 2) && (isLeapYear(year)== true)){
                return 29;
            } else if ((month == 2) && (isLeapYear(year) == false)){
                return 28;
            } else if (month ==3){
                return 31;
            } else if (month == 4){
                return 30;
            } else if (month == 5){
                return 31;
            } else if (month == 6){
                return 30;
            } else if (month == 7){
                return 31;
            } else if (month == 8){
                return 31;
            } else if (month == 9){
                return 30;
            } else if (month == 10){
                return 31;
            } else if (month == 11){
                return 30;
            } else if (month == 12){
                return 31;
            } else {
                return -1;
            }
        }

        public static int daysInMonth(int month, boolean isLeap){
            if (month == 1){
                return 31;
            } else if((month == 2) && (isLeap== true)){
                return 29;
            } else if ((month == 2) && (isLeap == false)){
                return 28;
            } else if (month ==3){
                return 31;
            } else if (month == 4){
                return 30;
            } else if (month == 5){
                return 31;
            } else if (month == 6){
                return 30;
            } else if (month == 7){
                return 31;
            } else if (month == 8){
                return 31;
            } else if (month == 9){
                return 30;
            } else if (month == 10){
                return 31;
            } else if (month == 11){
                return 30;
            } else if (month == 12){
                return 31;
            } else {
                return -1;
            }
        }

        public static boolean isValid(int month, int day, int year){
            if (year<= 0){
                return false;
            } else if(month>12){
                return false;
            } else if (month == 1 && day > 31){
                return false;
            } else if ((month == 2) && (isLeapYear(year)==true) && (day>29)){
                return false;
            } else if ((month == 2) && (isLeapYear(year)==false) && (day>28)){
                return false;
            } else if (month == 3 && day > 31){
                return false;
            } else if (month == 4 && day > 30){
                return false;
            } else if (month == 5 && day > 31){
                return false;
            } else if (month == 6 && day > 30){
                return false;
            } else if (month == 7 && day > 31){
                return false;
            } else if (month == 8 && day > 31){
                return false;
            } else if (month == 9 && day > 30){
                return false;
            } else if (month == 10 && day > 31){
                return false;
            } else if (month == 11 && day > 30){
                return false;
            } else if (month == 12 && day > 31){
                return false;
            } else{
                return true;
            }
        }

        public static int daysInYear(int year){
            if(isLeapYear(year)==true){
                return 366;
            } else {
                return 365;
            }
        }
        public static int dayOfYear(int month, int day, int year){
            int days = day;
            if (isLeapYear(year)==true){
                for(int i = 1; i<month; i++){
                    days += daysInMonth(i, true);
                }
            } else {
                for(int i = 1; i<month; i++){
                    days += daysInMonth(i, false);
                }
            }
            return days;
        }

        public static int daysToEndOfYear(int month, int day, int year){
            int daysLeft = 0;
            if (isLeapYear(year)==true){
                for(int i = month+1; i<=12; i++){
                    daysLeft += daysInMonth(i, true);
                }
                daysLeft += daysInMonth(month, true)-day;
            } else {
                for(int i = month+1; i<=12; i++){
                    daysLeft += daysInMonth(i, false);
                }
                daysLeft += daysInMonth(month, false)-day;
            }return daysLeft;
        }

        public static boolean isBefore(int m1,int d1,int y1,int m2,int d2,int y2){
            if(y2>y1){
                return true;
            } else if (y2==y1){
                if (m2>m1){
                    return true;
                } else if (m2<m1){
                    return false;
                } else {
                    if (d2>d1){
                        return true;
                    } else if (d2<d1){
                        return false;
                    } else{
                        return false;
                    }
                }
            } else {
                return false;
            }
        }

        public static int daysApart(int m1,int d1, int y1,int m2,int d2, int y2){
            if(isValid(m1,d1,y1) && isValid(m2,d2,y2)){
                int days = 0;
                if (y1<y2){
                    days += Math.abs(dayOfYear(m1,d1,y1)-dayOfYear(m2,d2,y2));
                    System.out.println(days);
                    for (int j = y1; j<y2; j++){
                        System.out.println(days);
                        days += daysInYear(j);

                    }   
                } else if (y2<y1){
                    days+= -Math.abs(dayOfYear(m1,d1,y1)-dayOfYear(m2,d2,y2));
                    System.out.println(days);
                    for (int j = y2; j<y1; j++){
                        System.out.println(days);
                        days += daysInYear(j);
                    }           
                } else{
                    days += Math.abs(dayOfYear(m1,d1,y1)-dayOfYear(m2,d2,y2));
                }
                return Math.abs(days);

            } else{
                return -1;
            }

        }
        public static void main(String[] args){
            // System.out.println(isLeapYear(2012));
            // System.out.println(daysInMonth(2,2012));
            // System.out.println(daysInMonth(2,true));
            // System.out.println(isValid(2,29,3));
            // System.out.println(isValid(2,29,2004));
            // System.out.println(isValid(13,13,2000));
            // System.out.println(daysInYear(2012));
            // System.out.println(dayOfYear(12,30,2014));
            // System.out.println(daysToEndOfYear(12,30,2014));
            // System.out.println(isBefore(5,20,1955,5,25,1955));
            // System.out.println(daysApart(1,1,2014,1,20,2014));
            // System.out.println(daysApart(1,20,2014,1,1,2014));
            // System.out.println(daysApart(1,1,1897,1,1,1898));
            System.out.println(daysApart(11,15,2004,1,15,2012));
        }
    }

2 个答案:

答案 0 :(得分:0)

如果这不是作业,请阅读CalendarDate个对象。实际上,无论如何你应该这样做。

这是错误:

 days += Math.abs(dayOfYear(m1,d1,y1)-dayOfYear(m2,d2,y2));

您不应该使用Math.abs。您还应将+=更改为-=。你在另一个分支中犯了同样的错误。

答案 1 :(得分:0)

如果第二个日期的月份和日期发生在您的第一个日期的月份和日期之前,即 11月15日 2004年和 1月15日 2012年,并且 9月25日 1932年和 6月21日 1993

在你的第一个if条件下,只需改变。如果还需要更改,请检查第二个条件。

  

天+ = Math.abs(dayOfYear(m1,d1,y1)-dayOfYear(m2,d2,y2));

  

天+ =(dayOfYear(m2,d2,y2) - dayOfYear(m1,d1,y1))