日期之间Java(作业)

时间:2011-01-25 10:22:22

标签: java algorithm datetime

这是我运行程序的几天假。关于我做错了什么建议?

我知道有一种更简单的方法可以做到这一点,但为此,我试图展示找到两者之间的所有实际步骤。

家庭作业,所以不能使用日期时间库。

public class DaysBetween {

    public static void main (String []args) {

        long months1 = Long.parseLong(args[0]);
        long days1 = Long.parseLong(args[1]);
        long year1 = Long.parseLong(args[2]);
        long months2 = Long.parseLong(args[3]);
        long days2 = Long.parseLong(args[4]);
        long year2 = Long.parseLong(args[5]);

        long daysbetween = 0; 
        long leapyearcounter = 0;
        boolean leapyear1 = false;
        boolean leapyear2 = false;    
        boolean valid1 = true;
        boolean valid2 = true;
        int earlier = 0;

        // this tests to see which date is earlier
        if (year1 == year2){ 
            if (months1 == months2) {
                if (days1 == days2) {
                    daysbetween = daysbetween;
                } else if (days1 < days2) {
                    earlier = 1;
                }
            } else if (months1 < months2) {
            earlier = 1;
            }
        } else if (year1 < year2 ) {
            earlier = 1;
        }

        // this switches the dates depending on which is earlier
        switch(earlier) { 
        case 1: months1 = Long.parseLong(args[0]);
            days1   = Long.parseLong(args[1]);
            year1   = Long.parseLong(args[2]);
            months2 = Long.parseLong(args[3]);
            days2   = Long.parseLong(args[4]);
            year2   = Long.parseLong(args[5]);
        break;  
        default: 
            months1 = Long.parseLong(args[3]);
            days1   = Long.parseLong(args[4]);
            year1   = Long.parseLong(args[5]);
            months2 = Long.parseLong(args[0]);
            days2   = Long.parseLong(args[1]);
            year2   = Long.parseLong(args[2]);
        break;
        }

        //this section tests if the earlier date is valid
        if((year1 % 4 == 0) && (year1 % 100 != 0) || (year1 % 400 == 0)) {
            leapyear1 = true;
        }

        if ((leapyear1 == true) && (months1 == 2)) {
            if (days1 <= 29 && days1 >=1){
                valid1 = true;
            }
        } else if ((leapyear1 == false) && (months1 == 2)){
            if (days1 <= 28 && days1 >= 1){
                valid1 = true;
            }
        }

        if (months1 != 2) {
            if (months1 < 8 ) {
                if (months1 %2 != 0) {
                    if (days1 <= 31 && days1 >=1){
                        valid1 = true;
                    } else {
                        valid1 = false;
                    }
                } else if (months1 %2 == 0)  {
                    if (days1 <= 30 && days1 >=1){
                        valid1 = true;
                    } else {
                        valid1 = false;
                    }
                }
            } else if (months1 > 8) {
                if (months1 %2 != 0) {
                    if (days1 <= 30 && days1 >=1){
                        valid1 = true;
                    } else {
                        valid1 = false;
                    }
                } else if (months1 %2 == 0)  {
                    if (days1 <= 31 && days1 >=1){
                        valid1 = true;
                    } else {
                        valid1 = false;
                    }
                }
            }
        }

        // this section tests if the later date is valid
        if((year2 % 4 == 0) && (year2 % 100 != 0) || (year2 % 400 == 0)) {
            leapyear2 = true;
        }

        if ((leapyear2 == true) && (months2 == 2)) {
            if (days2 <= 29 && days2 >=1){
                valid2 = true;
            }
        } else if ((leapyear2 == false) && (months2 == 2)){
            if (days2 <= 28 && days2 >= 1){
                valid2 = true;
            }
        }

        if (months2 != 2) {
            if (months2 < 8 ) {
                if (months2 %2 != 0) {
                    if (days2 <= 31 && days2 >=1){
                        valid2 = true;
                    } else {
                        valid2 = false;
                    }
                } else if (months2 %2 == 0)  {
                    if (days2 <= 30 && days2 >=1){
                        valid2 = true;
                    } else {
                        valid2 = false;
                    }
                }
            } else if (months2 > 8) {
                if (months2 %2 != 0) {
                    if (days2 <= 30 && days2 >=1){
                        valid2 = true;
                    } else {
                        valid2 = false;
                    }
                } else if (months2 %2 == 0)  {
                    if (days2 <= 31 && days2 >=1){
                        valid2 = true;
                    } else {
                        valid2 = false;
                    }
                }
            }
        }

        //this adds a day to the total if the earlier date is in january & its a leap year
        if ((months1 == 1) && (leapyear1 == true)) {
            daysbetween = daysbetween +1;
        }

        //this adds the months left in year1
        if (months1 == 1) { 
            daysbetween = daysbetween + 334;
        } else if (months1 == 2) {
            daysbetween = daysbetween + 306;
        } else if (months1 == 3) {
            daysbetween = daysbetween + 275;
        } else if (months1 == 4) {
            daysbetween = daysbetween + 245; 
        } else if (months1 == 5) {
            daysbetween = daysbetween + 214;
        } else if (months1 == 6) {
            daysbetween = daysbetween + 184;
        } else if (months1 == 7) {
            daysbetween = daysbetween + 153;
        } else if (months1 == 8) {
            daysbetween = daysbetween + 122;
        } else if (months1 == 9) {
            daysbetween = daysbetween + 92;
        } else if (months1 == 10) {
            daysbetween = daysbetween + 61;
        } else if (months1 == 11) {
            daysbetween = daysbetween + 31;
        } else { }

        // this adds the extra day if year2 is leap year and if the month is march or later
        if ((months2 >= 3) && (leapyear2 = true)) { 
            daysbetween = daysbetween + 1;
        }

        // this adds the months up to the month in year2
        if (months2 == 1) { 
            daysbetween = daysbetween;
        } else if (months2 == 2) {
            daysbetween = daysbetween + 31;
        } else if (months2 == 3) {
            daysbetween = daysbetween + 59;
        } else if (months2 == 4) {
            daysbetween = daysbetween + 90; 
        } else if (months2 == 5) {
            daysbetween = daysbetween + 120;
        } else if (months2 == 6) {
            daysbetween = daysbetween + 151;
        } else if (months2 == 7) {
            daysbetween = daysbetween + 181;
        } else if (months2 == 8) {
            daysbetween = daysbetween + 212;
        } else if (months2 == 9) {
            daysbetween = daysbetween + 243;
        } else if (months2 == 10) {
            daysbetween = daysbetween + 273;
        } else if (months2 == 11) {
            daysbetween = daysbetween + 304;
        } else  if (months2 == 12) {
            daysbetween = daysbetween + 334;
        } else { }

        //this add the days left in month1
        if (months1 != 2) { 
            if (months1 < 8 ) {
                if (months1 %2 != 0) {
                    daysbetween = daysbetween + (31 - days1);
                } else if (months1 %2 == 0) {
                    daysbetween = daysbetween + (30 - days1);
                }
            } else if (months1 > 8) {
                if (months1 %2 != 0) {
                    daysbetween = daysbetween + (30 - days1);
                } else if (months1 %2 == 0)  {
                    daysbetween = daysbetween + (31 - days1);
                }
            }
        }

        // this add the days left in month1 if its feb
        if ((leapyear1 == true) && (months1 == 2)) { 
            daysbetween = daysbetween + (29 - days1);
        } else {
            daysbetween = daysbetween + (28 - days1);
        }

        daysbetween = daysbetween + days2; // adds the days left in month2
        daysbetween = daysbetween + ((((year2-1)-(year1+1))+1)*365); // adds the days in the years to the total

        for (long i = (year1+1); i < (year2-1) ; i++) { // sees the # of leapyears b/n year1 & year2
            if ((i % 4 == 0) && (i % 100 != 0) || (i % 400 == 0)) {
                leapyearcounter++;
            }
        }

        daysbetween = daysbetween + leapyearcounter; // adds the leapyear days

        if ((valid1 == false) || (valid2 == false)) {
            System.out.println("Sorry! You gave me one or more invalid dates!");
        } else if (daysbetween == 1){
            System.out.println("There is " + daysbetween + " day between those two dates!");
        } else if (daysbetween != 1) {
            System.out.println("There are " + daysbetween + " days between those two dates!");
        } else if (daysbetween == 0) {
            System.out.println("There are no days between those two dates!");
        }
    }
}

3 个答案:

答案 0 :(得分:7)

除非你做了这个家庭作业或者只是一些自我伤害的事情,否则我建议你使用joda time library来计算所有日期时间:

时间使用joda回答:~22分钟

答案 1 :(得分:1)

这里有很多需要说明的内容,但我建议您首先将这个怪物重构为许多可以单独测试的小方法。由于数量庞大,您的代码难以阅读,理解和调试。 “分解”是一个有效的词。

您的主要方法应该只包含驱动程序或测试代码。

您应该拥有boolean isValid(Date date)boolean before(Date d1, Date d2)以及boolean isLeapYear(int year)等方法。单独测试这些并开始构建你想要的东西。

答案 2 :(得分:1)

使用 java.util.Date :( date1.getTime() - date2.getTime())/(1000 * 60 * 60 * 24)。