Days Between Dates Java (homework)
This is a couple days off when I run the program. Any advice on what I am doing wrong?
I know there is a simpler way to do it, but for this I'm trying to show all the actual steps in finding the days between.
Homework assignment, so cannot use date-time libraries.
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 mo开发者_开发技巧nth 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!");
}
}
}
Unless this homework or just some self hurting thing you have going can I recommend using the joda time library for all your datetime calculations:
Time to use joda answer: ~22 minutes
There's a lot that needs to be said here, but I'd recommend that you start by refactoring this monster into lots of smaller methods that you can test individually. Your code is difficult to read, understand, and debug because of the sheer volume. "Decomposition" is the operative word.
Your main method should only have driver or test code in it.
You should have methods like boolean isValid(Date date)
and boolean before(Date d1, Date d2)
and boolean isLeapYear(int year)
. Test these individually and start building up to what you want.
Use java.util.Date: (date1.getTime()-date2.getTime())/(1000*60*60*24).
精彩评论