Question: Need to make a Java program which calculates the number of days between two dates. I am NOT able to use anything too advanced yet
Need to make a Java program which calculates the number of days between two dates. I am NOT able to use anything too advanced yet such as import.java.util.Calender, import.java.util.Date; or import.java.util.GregorianCalender; Here are a couple of examples of what is should look like when complete. I am having troubles getting my calculations to come out correctly. Ive also written the code I have so far.
EXAMPLE
Enter first date now:
Enter Month: 1
Enter Day: 1
Enter Year: 1900
Enter the second date:
Enter Month: 1
Enter Day: 1
Enter Year: 2400
The first date is: 1/1/1900
The other date is: 1/1/2400
The total number of days between the dates is 182621
AND
Enter first date now:
Enter Month: 2
Enter Day: 23
Enter Year: 2016
Enter the second date:
Enter Month: 11
Enter Day: 30
Enter Year: 1955
The first date is: 2/23/2016
The other date is: 11/30/1955
The total number of days between the dates is 22000
Here is what I have so far:
Import java.util.Scanner;
Public class dates {
Public static void main ( String args [] ) {
int monthOne;
int monthTwo;
int dayOne;
int dayTwo;
intyearOne;
intyearTwo;
Scanner get = new Scanner (System.in );
System.out.println ( Enter first date now: );
do {
System.out.print ( Enter Month: );
monthOne = get.nextInt();
System.out.print ( Enter Day: );
dayOne = get.nextInt();
System.out.print ( Enter Year : );
yearOne = get.nextInt();
if (monthOne < 1 || monthOne > 12 ) {
System.out.printf ( %d is not a valid month , monthOne ); }
If ( (!(yearOne % 4 == 0 && yearOne & 100 !=0 || yearOne % 400 == 0) && (monthOne == 2 && dayOne >= 29)) || ((yearOne % 4 == 0 && yearOne % 100 != 0 || yearOne % 400 == 0) && (monthOne == 2 && dayOne >= 30))) {
System.out.printf ( %d is not a valid day. , dayOne ); }
If ( (monthOne == 4 && dayOne > 30) || (monthOne ==6 && dayOne > 30) || (monthOne == 9 && dayOne > 30) || (monthOne == 11 && dayOne > 30) || (dayOne < 1 || dayOne > 31)) {
System.out.printf ( %d is not a valid day. , dayOne );
If ( yearOne < 1900 || yearOne > 2400 ) {
System.out.printf ( %d is not a valid year. , yearOne ); }
} while (( (!(yearOne % 4 == 0 && yearOne % 100 !=0 || yearOne % 400 == 0) && (monthOne == 2 && dayOne >= 29)) || ((yearOne % 4 == 0 && yearOne % 100 != 0 || yearOne % 400 == 0) && (monthOne == 2 && dayOne >= 30 )) || (monthOne == 4 && dayOne > 30) || (monthOne == 6 && dayOne > 30) || (monthOne == 9 && dayOne > 30) || (monthOne == 11 && dayOne > 30) || (monthOne < 1 || monthOne > 12) || (dayOne < 1 || dayOne > 31) || (yearOne < 1900 || yearOne > 2400) ));
System.out.printf ( Enter the second date: );
do {
System.out.print ( Enter Month: );
monthTwo = get.nextInt();
System.out.print ( Enter Day: );
dayTwo = get.nextInt();
System.out.print ( Enter Year : );
yearTwo = get.nextInt();
if (monthTwo < 1 || monthTwo > 12 ) {
System.out.printf ( %d is not a valid month , monthTwo ); }
If ( (!(yearTwo % 4 == 0 && yearTwo & 100 !=0 || yearTwo % 400 == 0) && (monthTwo == 2 && dayTwo >= 29)) || ((yearTwo % 4 == 0 && yearTwo % 100 != 0 || yearTwo % 400 == 0) && (monthTwo == 2 && dayTwo >= 30))) {
System.out.printf ( %d is not a valid day. , dayTwo ); }
If ( (monthTwo == 4 && dayTwo > 30) || (monthTwo ==6 && dayTwo > 30) || (monthTwo == 9 && dayTwo > 30) || (monthTwo == 11 && dayTwo > 30) || (dayTwo < 1 || dayTwo > 31)) {
System.out.printf ( %d is not a valid day. , dayTwo );
If ( yearTwo < 1900 || yearTwo > 2400 ) {
System.out.printf ( %d is not a valid year. , yearTwo ); }
} while (( (!(yearTwo % 4 == 0 && yearTwo % 100 !=0 || yearTwo % 400 == 0) && (monthTwo == 2 && dayTwo >= 29)) || ((yearTwo % 4 == 0 && yearTwo % 100 != 0 || yearTwo % 400 == 0) && (monthTwo == 2 && dayTwo >= 30 )) || (monthTwo == 4 && dayTwo > 30) || (monthTwo == 6 && dayTwo > 30) || (monthTwo == 9 && dayTwo > 30) || (monthTwo == 11 && dayTwo > 30) || (monthTwo < 1 || monthTwo > 12) || (dayTwo < 1 || dayTwo > 31) || (yearTwo < 1900 || yearTwo > 2400) ));
System.out.printf ( The first date is: %d/%d/%d , monthOne, dayOne, yearOne );
System.out.printf ( The other date is: %d/%d/%d , monthTwo, dayTwo, yearTwo );
int totalDayOne = 0;
int numDaysJan = dayOne;
int numDaysFeb = 31 + dayOne;
int numDaysMar = 31 + 28 + dayOne;
int numDaysApr = 31 + 28 + 31 + dayOne;
int numDaysMay = 31 + 28 + 31 + 30 + dayOne;
int numDaysJun = 31 + 28 + 31 + 30 + 31 + dayOne;
int numDaysJul = 31 + 28 + 31 + 30 + 31 + 30 + dayOne;
int numDaysAug = 31 + 28 + 31 + 30 + 31 + 30 + 31 + dayOne;
int numDaysSep = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + dayOne;
int numDaysOct = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + dayOne;
int numDaysNov = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + dayOne;
int numDaysDec = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + dayOne;
int totalDayTwo = 0;
int numDaysJanTwo = dayTwo;
int numDaysFebTwo = 31 + dayTwo;
int numDaysMarTwo = 31 + 28 + dayTwo;
int numDaysAprTwo = 31 + 28 + 31 + dayTwo;
int numDaysMayTwo = 31 + 28 + 31 + 30 + dayTwo;
int numDaysJunTwo = 31 + 28 + 31 + 30 + 31 + dayTwo;
int numDaysJulTwo = 31 + 28 + 31 + 30 + 31 + 30 + dayTwo;
int numDaysAugTwo = 31 + 28 + 31 + 30 + 31 + 30 + 31 + dayTwo;
int numDaysSepTwo = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + dayTwo;
int numDaysOctTwo = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + dayTwo;
int numDaysNovTwo = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + dayTwo;
int numDaysDecTwo = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + dayTwo;
if ( monthOne == 1 ) {
totalDayOne += numDaysJan;
}
if ( monthOne == 2 ) {
totalDayOne += numDaysFeb;
}
if ( monthOne == 3 ) {
totalDayOne += numDaysMar;
}
if ( monthOne == 4 ) {
totalDayOne += numDaysApr;
}
If ( monthOne == 5 ) {
totalDayOne += numDaysMay;
}
if (monthOne == 6 ) {
totalDayOne += numDaysJun;
}
if ( monthOne == 7 ) {
totalDayOne += numDaysJul;
}
if ( monthOne == 8 ) {
totalDayOne += numDaysAug;
}
if ( monthOne == 9 ) {
totalDayOne += numDaysSep;
}
if ( monthOne == 10 ) {
totalDayOne += numDaysOct;
}
if (monthOne == 11 ) {
totalDayOne += numDaysNov;
}
if (monthOne == 12 ) {
totalDayOne += numDaysDec;
}
if ( monthTwo == 1 ) {
totalDayTwo += numDaysJanTwo;
}
if ( monthTwo == 2 ) {
totalDayTwo += numDaysFebTwo;
}
if ( monthTwo == 3 ) {
totalDayTwo += numDaysMarTwo;
}
if ( monthTwo == 4 ) {
totalDayTwo += numDaysAprTwo;
}
If ( monthTwo == 5 ) {
totalDayTwo += numDaysMayTwo;
}
if (monthTwo == 6 ) {
totalDayTwo += numDaysJunTwo;
}
if ( monthTwo == 7 ) {
totalDayTwo += numDaysJulTwo;
}
if ( monthTwo == 8 ) {
totalDayTwo += numDaysAugTwo;
}
if ( monthTwo == 9 ) {
totalDayTwo += numDaysSepTwo;
}
if ( monthTwo == 10 ) {
totalDayTwo += numDaysOctTwo;
}
if (monthTwo == 11 ) {
totalDayTwo += numDaysNovTwo;
}
if (monthTwo == 12 ) {
totalDayTwo += numDaysDecTwo;
}
int daysBetween = totalDayOne totalDayTwo;
if ( totalDayOne < totalDayTwo ) {
daysBetween = totalDayTwo totalDayOne;
}
int daysTotal = 0;
while ( yearOne < yearTwo ) {
yearOne++;
if ( yearOne % 4 == 0 && yearOne % 100 != 0 || yearOne % 400 == 0)
daysTotal += 366;
else
daysTotal += 365;
}
while ( yearTwo < yearOne ) {
yearTwo++;
if ( yearTwo % 4 == 0 && yearTwo % 100 != 0 || yearTwo % 400 == 0)
daysTotal += 366;
else
daysTotal += 365;
}
System.out.printf ( The total number of days between the dates is %d , daysTotal + daysBetween);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
